Three-times Miami champion Federer finished with 38 winners but had to overcame 41 unforced errors to defeat Radu Albot.
David Ferrer is set to retire from professional tennis in May but the Spaniard showed he still has the tools to bring down the biggest names in the game after a stunning 2-6, 7-5, 6-3 win over World No. 3 Alexander Zverev at the Miami Open on Saturday.
The victory clearly meant a lot to the 36-year-old, who will bring the curtain down on his career at the Madrid Open.
"It's a special day because it's the last year of professional tennis for me. Winning these type of matches against a top 10 player like 'Sascha' is a gift," he said.
"I'm very happy and I'm trying to enjoy every point and every moment."
Ferrer will next face 21-year-old American Frances Tiafoe who fought past Miomir Kecmanovic 7-6(6), 7-6(5).
Earlier on Saturday, South Africa’s Kevin Anderson made a winning return to competition with a 6-4, 3-6, 6-3 victory over rising Spaniard Jaume Munar. After more than two months away from the court with an elbow injury, the world number seven smashed 17 aces en route to the second round win over the 21-year-old Munar.
It was Anderson's first tournament appearance since the Australian Open in January and he said he was happy with the way his elbow felt.
"To get through in three sets, it felt really good to be back out there. I’m happy with how the elbow responded. I feel like I’m hitting the ball great," the sixth seed added.
Two Russian prospects, Karen Khachanov and Daniil Medvedev, had different fates on Saturday.
The 10th seed Khachanov was beaten by Australian Jordan Thompson 6-2, 6-3 while his friend Medvedev, the 13th seed, toppled France’s Adrian Mannarino 6-2, 6-1.
Eighth seed Stefanos Tsitsipas beat Mackenzie McDonald 7-6(4), 6-1.
In other early action, Belgian David Goffin, the 18th seed, defeated Spaniard Pablo Andujar 6-4, 6-1, while Canadian 20th seed Denis Shapovalov came from a set down to beat Britain’s Dan Evans 4-6, 6-1, 6-3.
Roger Federer, a year older than Ferrer but enjoying a career renaissance, rallied from a set down to beat Moldovan surprise package Radu Albot 4-6, 7-5, 6-3 in a stuttering start to the 20-times Grand Slam champion's Miami Open campaign.
Federer, beaten by Dominic Thiem in last week's BNP Paribas Open final, smashed 14 aces and broke Albot twice to advance to the third round but it was far from plain sailing for the 37-year-old against the world number 46.
"I expected a player with a great attitude, which he showed,” Federer said. “I have a lot of respect for those types of players who don't have the size, have to find a different way to win. He's a great, great player. I was impressed.”
Federer was stunned by Thanasi Kokkinakis in his opening match of the Miami Open last year and another early exit was beckoning when he dropped the first set on Saturday.
However, the Swiss battled back to break Albot while leading 6-5 in the second with a backhand volley winner to square the match. Albot could not convert a break point in the deciding set when tied a 3-3, and Federer took command from there to wrap up a match that lasted two hours and 10 minutes. Three-times Miami champion Federer finished with 38 winners but had to overcame 41 unforced errors to defeat Albot, who earned an ovation from the crowd for his efforts.
Federer will next face Serbia's Filip Krajinovic on Monday.
Osaka stunned by Hsieh
The Miami Open lost two of its biggest drawcards on Saturday with world number one Naomi Osaka suffering a shock third-round loss to Taiwan’s Hsieh Su-wei after eight-times champion Serena Williams withdrew due to a knee injury.
The 33-year-old Hsieh came from a set down to prevail 4-6, 7-6(4), 6-3 as her unorthodox style frustrated, perplexed and finally wore down U.S. and Australian Open champion Osaka.
Osaka, who lost in the fourth round at Indian Wells and could lose her number one ranking depending on how others do in the tournament, said the mental side of her game had let her down.
“I just think I got too emotional, which is something I did last match, too,” she said. “I would just say I was kind of immature today. I was thinking too much.”
Hsieh had lost her two previous matches with Osaka, including a hard-fought contest in Melbourne in January, but came back from a break down in each of the last two sets, saving eight of the 12 break points she faced.
The Taiwanese had knocked out then world number one Simona Halep at Wimbledon last year and again demonstrated her range of clever shot making against a much more powerful opponent.
Osaka looked on course for a straightforward win after she broke for a 5-4 lead in the opener then delivered an ace to serve out the set.
Serving for the match at 5-4 in the second, Osaka’s charge began to wilt and Hsieh fought back with some brilliant tennis to take it to a tiebreak before levelling the match with a lovely passing winner.
At 3-3 in the third set, Hsieh took advantage of Osaka’s ineffective serve and mounting error count to get a key break and had no trouble holding for a 5-3 lead.
A delightful dropshot winner finally put paid to an exasperated Osaka’s resistance, sealing another break and the victory after two hours and 18 minutes of play.
Osaka had won 63 straight matches after taking the first set but Hsieh’s victory brought that run to an end and set up a fourth round meeting with Caroline Wozniacki, who beat Romanian qualifier Monica Niculescu 6-4, 7-6(4).
Earlier, world number two Petra Kvitova needed three sets and more than two and a half hours to beat Croatia’s Donna Vekic 6-4, 3-6, 6-4 in a clash of heavy hitters.
The two-time Wimbledon champion seed will meet Caroline Garcia in the fourth round after the Frenchwoman knocked out 15th seeded German Julia Goerges 6-0, 7-5.
Knee injury forces Serena out
Serena Williams withdrew from the Miami Open with a left knee injury, tournament officials said on Saturday.
It is the second consecutive WTA event in which the American has been unable to continue, after her retirement from last week’s tournament in Indian Wells with a viral illness.
“I am disappointed to withdraw from the Miami Open due to a left knee injury,” the former world number one said in a statement.
“I hope to be back next year to play at this one-of-a-kind tournament in front of the incredible fans here in Miami.”
The eight-times Miami champion had begun the tournament with a tough three-set victory over Sweden’s Rebecca Peterson, winning 6-3, 1-6, 6-1 on Friday.
“I knew I could play a lot better than I was playing,” Williams had said afterwards of her error-ridden second set.
At Indian Wells, Williams had claimed a 7-5, 6-3 victory over two-time Grand Slam winner Victoria Azarenka before being forced to retire from her third round match against Garbine Muguruza, when trailing 6-3, 1-0.