Federer, seeded second behind Novak Djokovic and chasing his first title since Cincinnati last August, could get a quarter-final clash with his great rival Rafael Nadal, who was to play Latvian qualifier Ernests Gulbis in the fourth round later Wednesday.
Federer improved to 13-1 against Wawrinka, but he had to labor two hours and 20 minutes to do so.
Serving for the match at 5-4 in the second set he was broken at love. Unable to convert two break points in the following game he surrendered the set on a tiebreaker when he double faulted on set point.
Wawrinka then gained the upper hand with a break in the third set, but Federer, owner of 17 Grand Slam titles broke back in the next game and earned the decisive break in the final game as Wawrinka smacked a volley into the net.
"I definitely got a bit lucky out here tonight," Federer said. "Stan's an amazing player and a good friend - I'm happy to get through."
Results on Wednesday from the eighth day of the ATP Masters and WTA BNP Paribas Open (x denotes seeded player):
Men
Fourth round
Jo-Wilfried Tsonga (FRA x8) bt Milos Raonic (CAN x17) 4-6, 7-5, 6-4
Kevin Anderson (RSA) bt Gilles Simon (FRA x13) 6-3, 1-6, 6-4
Tomas Berdych (CZE x6) bt Richard Gasquet (FRA x10) 6-1, 7-5
Roger Federer (SUI x2) bt Stanislas Wawrinka (SUI x18) 6-3, 6-7 (4/7), 7-5
Andy Murray (GBR x3) bt Carlos Berlocq (ARG) 7-6 (7/4), 6-4
Juan Martin del Potro (ARG x7) bt Tommy Haas (GER x19) 6-1, 6-2
Rafael Nadal (ESP x5) bt Ernests Gulbis (LAT) 4-6, 6-4, 7-5
Women
Quarter-finals
Maria Kirilenko (RUS x13) bt Petra Kvitova (CZE x5) 4-6, 6-4, 6-3
Maria Sharapova (RUS x2) bt Sara Errani (ITA x6) 7-6 (8/6), 6-2