Federer was broken when serving for the match at 5-3 and was unable to convert on match-point at in the next game.
Federer, who had won his last six against Berdych, had his set streak of 15 against the 31-year-old come to an end.
It had looked as if he would have little trouble with Berdych, who he has not lost to since 2013, after a routine first set.
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.
Nick Kyrgios is determined to make the most of his Miami Open semi-final clash against Roger Federer, who he described as the "greatest of all time".
But Federer's class proved the difference in the tiebreaks as he improved to 6-0 lifetime against Agut and advanced to a quarter-final match against Czech 10th seed Tomas Berdych. Then you see how much I'm frustrated.
Federer moved a game away from victory when he consolidated the break courtesy of a complete mishit from Berdych. Kyrgios has the ability to beat anybody in the world with the way he's playing and is always tempting as an underdog.
That one forehand miss, forced by a hard return, ended up being the last groundstroke Berdych got to hit in the match.
Awaiting him is the mercurial Kyrgios, who won his own quarter-final against Alexander Zverev 6-4, 6-7 (11-9), 6-3. "It's obviously not something that you want, to finish the tournament like that. I have to bring 100 per cent", said Kyrgios.
"They like the big stage", Federer answered when asked about Kyrgios and Zverev before Thursday's second quarterfinal.
The victor will play Federer, who has now won 17 of his 18 matches this year, in the last four.
In the bottom half of the draw, unseeded Italian Fabio Fognini will face Spain's fifth-seed Rafael Nadal for a spot in the final.
With no margin for error, Kyrgios was again ahead of breaking the serve of his rival in the sixth game and confirmed the break in the next to get 5-2, distance that the German could not recover and he said goodbye to the tournament Three weeks before turning 20 on April 20.
"I know him very well", Nadal said.