The Swiss maestro was staring at a 6-4 deficit in the third set tie-break but held his nerve to complete a 6-2, 3-6, 7-6(6) victory.
The 35-year-old was given a shock to the system when he dropped his first set in three weeks after coasting through the opening half an hour.
The pair traded breaks in set three and the world number six coughed up the chance to serve for a semifinal berth at 5-3.
Berdych, whose form looks to be steadily improving, scurried down to the locker room in a hurry after double-faulting to gift Federer the victory.
Plain sailing initially
Neither player had dropped a set in the build-up to their showdown on Stadium Court but few could argue that Federer held the advantage.
He had blitzed the Czech off the court at the Australian Open en route to his 18th major title in January and their head-to-head record sided heavily with the Swiss.
Federer, despite wrestling with various ailments in recent years, had claimed their previous six encounters and, more harrowingly for Berdych, the last fourteen sets.
And when the former world number one grabbed a double break and closed out the opening set in just under half an hour to rack up the total set count to fifteen, the Czech could be forgiven for experiencing a looming sense of déjà vu.
In fact, Federer was toying with his opponent as the clouds began gave way to the sun in Key Biscayne.
It took him only a game to find his rhythm and break Berdych and the aggressive, net-rushing style that bore success for him in Indian Wells last week was proving a treat.
Berdych steadied himself and was beginning find his groove until the sixth game of the set.
That’s when Federer produced one of the shots of the year on break point when his slice-cum-drop shot off the backhand wing caught the Czech flat-footed on the baseline and the crowd leapt to their feet in adulation.
A second serve ace secured the opening set for the Swiss but the script was to be torn up.
Berdych wasn’t finding many first serves to open set two but when he did, Federer had little to no answer.
Indeed, the set stayed on serve until the seventh game until, to the dismay of many who have witnessed Federer’s renaissance in 2017, the Czech broke.
Federer had remained solid throughout but the unforced errors were beginning to creep in and an errant backhand had Berdych fist pumping.
The 31-year-old was not ready to pass up the opportunity and leveled proceedings just as the game clock struck an hour.
There were to be further twists and Berdych almost broke for 2-0 to begin set three but Federer, so reliable from his forehand all afternoon, managed to rescue it.
And it would be his forehand off the return that fashioned a break of serve at 3-2; the Czech finding the depth too great and netting a low backhand.
What was to follow was inconceivable as Federer contrived to double fault and moonball a forehand at 5-3 to gift his opponent a route back into the match.
Both players regained their poise to stage a tense tie-break for a place in the semifinal.
Trading early mini-breaks, it was Federer who committed one more error that had Berdych staring at a place in the semifinal for the first time since 2015.
He could not convert and when the tables were turned and he was down a match point on his second serve, his brain led him astray.