The seven-time Wimbledon champion was made to fight by his Swiss compatriot but after pulling level at one set all, Federer eased through the deciding set to seal a 4-6 7-6 (7/4) 6-0 victory and a quarter-final showdown with Marin Cilic.
The win means Novak Djokovic, also a winner on Thursday, cannot replace Federer as world number one when the new ranking list is published on Monday. Federer tally of weeks at the summit of his sport will therefore tick over onto a remarkable 300.
Wawrinka took the first set with relative ease, winning 70 per cent of points on his serve.
He had chances to go a break up in the second but could not take them and a tense tie-break followed.
Federer capitalised on his opponent's sudden inability to return the ball to earn a final set which he duly dominated.
Watch: Federer survives scare, Djokovic cruises... More Videos
Federer, whose quarter-final foe Cilic also had to recover from a set down to dismiss Spain's Fernando Verdasco 4-6 6-1 6-4, told Sky Sports: "He didn't give me too many opportunities and I knew it would be extremely difficult.
"I'm relieved to get through and be moving on to give myself another opportunity.
"And if the opportunity presents itself, I'd love to finish the year as world number one."
Djokovic, however, is favourite to finish 2012 in top spot. He beat Feliciano Lopez 6-3 6-3.
The Serb broke his opponent three times to set up a quarter-final meeting with 34-year-old Tommy Haas, who saw off Janko Tipsarevic 6-2 6-1.
A proliferation of aces from Djokovic's compatriot were not enough to save him from a third-round exit at the hands of the German veteran.
France's Jo-Wilfried Tsonga booked his progression to the next round after coming out on top in an exciting battle against former Australian Open finalist Marcos Baghdatis.
There were eight breaks of serve as Tsonga struck 11 aces to oust the Cypriot 6-2 7-6 (7/2).
The Frenchman will now face Tomas Berdych of the Czech Republic, who fended off a barrage of 17 aces from Sam Querrey to win 6-2 6-7 (3/7) 6-4.