The 35-year-old Swiss, who has already won the Australian Open and Indian Wells so far this year, extended his win streak over Berdych to seven matches.
There was plenty of tension in the match-deciding tie-breaker, where Federer saved match point and then Berdych lost on a double-fault.
"I definitely got very lucky at the end, but I think I showed great heart today," Federer said. "I fought and Tomas definitely started to step it up, but it was a great match at the end."
Federer will face Nick Kyrgios in the semi-finals after the Australian came through an entertaining 6-4, 6-7 (9/11), 6-3 victory over 19-year-old German Alexander Zverev.
Should Federer win on Friday, there is a chance he will play Sunday's final against long-standing rival Rafael Nadal, who faces Italy's Fabio Fognini in the other semi-final.
But Thursday could very easily have been the end of the tournament for the 18-time Grand Slam champion, who entered the match having not dropped a set over eight consecutive matches at Indian Wells and Miami.
Berdych was broken in the opening game of the match and then Federer dealt with a break point in the second game. The Swiss was comfortable on his serve and then broke Berdych again at 4-2 to put the opening set beyond doubt.
But then, the Czech dug deep and came out fighting.
Berdych broke to go 5-3 up and took the contest into a third set, where Federer made the most of some unforced errors from his opponent to break at 4-2 but then, uncharacteristically, Federer let some sloppiness creep into his game.
Federer was broken when serving for the match at 5-3 and was unable to convert on match-point at in the next game.
Berdych led 6-4 in the tie-breaker but gave up two points and Federer blasted a superb ace to get in front before the double-fault from the Czech ended the contest.
It was the second demoralizing defeat this month for Berdych. He lost at Indian Wells in the third round after leading 6-1, 5-2 against Japan's Yoshihito Nishioka.
Asked about his frustration, Berdych said: "What should I do? Should I destroy the table? Then you see how much I'm frustrated. I don't know.
"It's obviously not something that you want, to finish the tournament like that. But I got a lesson in the last tournament, so this is going to be fine to get over with. I think this is going to be a strawberry cake after that. It's going to be fine."
Federer improved to 17-1 in 2017, his best start to a season since 2006, and has now won seven tie-breakers in a row.
The Kyrgios-Zverev quarter-final was billed as a glimpse at the future of top-level tennis and if so, the sport should have an entertaining era ahead.
The match featured huge serves from Kyrgios, audacious and expertly executed lobs from Zverev and an intense second set tie-breaker decided for the German after his rival tried unsuccessfully to return a lob on set point through his legs.
Kyrgios, though, has added greater control to his always explosive game and finished strong to set up a fascinating clash with Federer.