Having seen Rafael Nadal bow out of the tournament, Federer seemed to have an easy route to the semi-finals, but Ukrainian Stakhovsky stunned the Swiss with a 6-7 (5/7) 7-6 (7/5) 7-5 7-6 (7/5) win in the second round.
Federer's earliest exit at Wimbledon for 11 years comes as a huge boost for Britain's Andy Murray, who earlier saw his potential quarter-final opponent Jo-Wilfried Tsonga pull out through injury.
With Maria Sharapova and Jo-Wilfried Tsonga exiting the tournament earlier, the Wimbledon crowds must have thought they had seen all the day's shocks.
Even the most optimistic of Stakhovsky supporters could not have seen this dramatic Centre Court result - one of the biggest shocks in Wimbledon history - coming.
The 27-year-old, who has only made the second round here once before, played a superb serve-and-volley game that ended Federer's run of 36 consecutive grand slam quarter-final appearances.
Stakhovsky was good value for the win, with Federer having no answer to his opponent's movement and precision.
Speaking to the BBC in the immediate aftermath, Stakhovsky said: "I am in disbelief that it happened. It was the best tennis I played and incredible.
"When you play Roger Federer at Wimbledon he's historical, it's like two against one. I hoped he wouldn't get too far from me, that was my plan.
"I hung in there, tried to go to tie-breaks. I got a little tight when I got up a break in the fourth and I am really happy to go through.
"It's magic. I couldn't play any better. I did everything I needed, it was a fantastic day for me."
Federer strode on to court in his typically confident manner, yet Stakhovsky seemed unfazed by the occasion.
After a tight first set, a tie-break came and Federer took first blood, playing Stakhovsky at his own game by advancing to the net to drop a forehand winner. Stakhovsky saved two set points but Federer responded with an ace to take a difficult opener after 45 minutes.
Both players' armour looked more vulnerable in the second set. Federer earned one break point but he was unable to take it while Stakhovsky was guilty of missing another two chances to break.
Another tie-break arrived, but this time it was Stakhovsky who came out on top.
The Ukrainian broke Federer at the first opportunity when the Swiss netted a simple backhand.
Federer levelled the tie-break with a big return but he went 6-5 behind and Stakhovsky claimed the set after smashing at the net.
The Centre Court crowd willed the seven-time champion on and he responded by winning two break points at the start of the third set.
The 31-year-old was unable to convert them though, sending two forehands beyond the base line.
Stakhovsky gained confidence and held to love before earning a break point of his own in the seventh game, but Federer held strong.
Federer sent down an ace to save a break point at 5-5, but Stakhovsky had one more chance and he took it when the champion mis-hit a simple backhand.
Stakhovsky again showed nerves of steel to serve out for the third set, clinching the game when Federer ploughed a shot into the net.
Stakhovsky punched the air in celebration and then gathered his composure to begin the fourth.
There was no letting up from Stakhovsky as he made a breakthrough in the champion's second service game.
Stakhovsky sped around the court in a tense rally and lobbed a simple shot to Federer's left, but the Swiss inexplicably netted to give his opponent a 2-1 lead.
Federer wasted two chances to break back in the fourth game but he levelled the set in the sixth to make it 3-3.
A slip from Stakhovsky knocked him out of his stride, and he gave Federer a set point, but the underdog saved by volleying a winner from the net.
Stakhovsky broke early in the tense fourth-set tie-break and Federer sent a straightforward backhand wide at 6-5 down to seal his own demise.