Georgian Basilashvili saved a match point in the final set and took advantage of a tiring Federer to reach the semi-finals.
“It’s unbelievable, he’s one of the greatest of all time. Just playing him means so much to me,” Basilashvili said.
“I’m very happy he’s back playing again. To win against him is a dream come true, he was always my idol. He’s an unbelievable player.”
Federer, 39, was pushed to the limit in his first match back on Wednesday when he beat Briton Dan Evans in three sets but the Swiss looked well in control in the opening set against the unseeded Basilashvili.
The 20-times Grand Slam champion broke Basilashvili early and saved three break points to take a 4-1 lead.
Federer, returning to action after two knee operations last year, punished Basilashvili with vintage backhand winners down the line and trademark sliced backhand drop shots.
Basilashvili fought back in the second set, however, saving three break points on the way to a 3-0 lead before he wrapped up the set in 25 minutes with the help of seven aces.
Federer looked tired in the third set and at 3-3 he had to fight hard to hold his serve in a lengthy game.
Basilashvili saved a match point and won seven of the next eight points to take a 6-5 lead and he served out for the match, clinching victory with a backhand winner.
Basilashvili will meet Taylor Fritz in the semi-finals after the American beat fourth seed Denis Shapovalov 5-7 6-3 7-5.