The shock 6-7 (5/7), 7-6 (7/5), 7-5, 7-6 (7/5) loss in a thrilling, three-hour duel on Centre Court brought the curtain down on seven-time Wimbledon champion Federer's extraordinary record in the sport's four majors.
The Wimbledon third seed had made 36 consecutive Grand Slam quarter-final appearances, having not gone out of a major before the last eight since the 2004 French Open.
It was his earliest Wimbledon defeat since he was knocked out in the first round in 2002 by Mario Ancic. The following year he lost in the first round of the French Open to Luis Horna.
"I'm very disappointed. Losing at Wimbledon always has been that way, will never change. So just got to get over this one. Some finals haven't hurt this much, that's for sure," Federer said.
"It's always a disappointment losing any match around the world, and particularly here. I've had some great moments here, but also some tougher ones.
"I struggled maybe on the big points this time around again, like I have for some time this season. "It was tricky. But credit to him for closing it out under enormous pressure. He was better in the more important points today than I was.
"I thought I had my opportunities, had the foot in the door. When I had the chance, I couldn't do it. It's very frustrating, very disappointing. "I'm going to accept it and move forward from here. I have no choice."
Stakhovsky was struggling to take in his victory over the 17-time Grand Slam champion. "I'm still in disbelief that it actually happened," said the 27-year-old, who does not even have a coach.
"I played my best tennis and still it almost wasn't enough. I'm incredibly happy." It was the first time that he had ever beaten a top 10 player in 21 attempts.
"When you play Roger at Wimbledon it's like you play two people. First you play his ego then as well on Centre Court where he is historic. It is like two against one," he said.
"I don't know how to describe it. Magic. I couldn't play any better. "Everything I needed went right. I volleyed incredibly well. It was a fantastic day."
Federer's defeat leaves the bottom half of the draw wide open for Britain's second seed Andy Murray, with two-time champion Rafael Nadal already out and sixth seed Jo-Wilfried Tsonga withdrawing through injury.
Spain's 15th seed Nicolas Almagro is the highest-ranked player remaining in the bottom half. Federer took the first set on the tie-break and it looked at that point as though the 31-year-old would cruise his way into the third round.
But Stakhovsky had other ideas and pushed Federer all the way to a tie-break in the second set, and won it. When the Ukrainian won the third set the buzz went around Wimbledon that one of the sport's greats was on his way out from the place he has made his stomping ground over the past decade.
At 4-4 in the fourth set, the frustration finally got to the ice-man Federer, who blasted a ball that had been called out straight towards his opponent, who was forced to duck. The Swiss apologised.
Federer had set point in the fourth -- the sort of turning point where the Swiss star's cool experience has got him out of trouble in the past. However, Stakhovsky took it to a third tie-break and earned himself two match points.
Federer pulled off a stunning shot to stave off the first but shot wide on the next one and his title defence was over. Both players went off to a huge ovation from the 15,000 fans crammed into Centre Court.
The polyglot son of a urology professor and a retired university economics teacher, Stakhovsky spends his time off reading Russian classics and playing billiards, among other pursuits.
He faces Austria's unseeded Jurgen Melzer for a place in the last 16.
Photos: Salman Khan returns from Jodhpur to Mumbai
Pics: Sidharth Shukla-Rashami Desai's sizzling chemistry at TV show launch
Ooh La Lana! WWE Diva flaunts sexy curves in these 30 pictures
15 'peach' perfect photos of Priyanka Chopra at People's Choice Awards 2017
62 going on 16: These photos of Rekha prove she is 'forever young'