The 27-year-old Ukrainian outplayed Federer on Centre Court, serving and volleying his way to a 6-7 (5), 7-6 (5), 7-5, 7-6 (5) victory that stands out as one of the biggest upsets in Grand Slam history.
"Magic," Stakhovsky said. "I couldn't play any better today."
The result capped a chaotic day at Wimbledon when seven players were forced out by injuries, and former champion Maria Sharapova fell in the second round to a qualifier. Seven former No. 1 -ranked players all departed the tournament Wednesday.
Federer's loss ended his record streak of reaching at least the quarterfinals at 36 consecutive Grand Slam tournaments, a run that began at Wimbledon in 2004, shortly after a third-round exit at that year's French Open.
"It's always a disappointment losing any match around the world, and particularly here," Federer said. "I've had some great moments here, but also some tougher ones. Can't have 'em all. It was a tough loss today."
The owner of a record 17 major championships, Federer hadn't been beaten in the second round or earlier since a first-round defeat at the 2003 French Open.
The 31-year-old Federer said he won't "panic" and will work hard to come back stronger.
"I'm looking forward to what's to come," he said. "Looking forward to next year, that I can do better next year. "
Federer said the end of his quarterfinal run does not represent the end of an era.
"I still have plans to play for many more years to come," he said. " It's normal that after all of a sudden losing early after being in the quarters 36 times, people feel it's different. "
Federer's shock defeat was his earliest at the All England Club since a first-round loss in 2002 to No. 154-ranked Mario Ancic. Stakhovsky is the lowest-ranked player to beat Federer at any event since then.
Wednesday's defeat came on the same grass court Federer has made his own for nearly a decade.
It ended with Stakhovsky converting on his second match point, a 13-stroke rally that finished with Federer hitting a backhand wide.
Stakhovsky fell onto his back in celebration. He later bowed to the crowd as Federer walked off the court with a quick wave.
Federer converted only one of eight break points against Stakhovsky, who broke the Swiss star twice. The Ukrainian piled up 72 winners against 17 unforced errors, while Federer had 56 winners and 13 errors.
While few play serve-and-volley these days, Stakhovsky used the tactic with great success throughout the match to keep Federer off balance. He won 61 out of 96 points at the net.
"I'm still in disbelief," Stakhovsky said. "When you play Roger Federer at Wimbledon it's like you are playing two persons. First you play Roger Federer, then you play his ego, and on the Centre Court of Wimbledon, where he is historical. So that's like playing two against one."
Federer's defeat was the biggest shock on a day full of them.
Earlier, third-seeded Sharapova, the 2004 Wimbledon champion, was stunned 6-3, 6-4 by 131st-ranked Michelle Larcher de Brito of Portugal in the second round.
Sharapova slipped and fell several times on the grass on Court 2 and received medical treatment from the trainer in the second set.
It wasn't serious enough to force Sharapova to quit, as so many others did Wednesday either by walkover or mid-match retirements.
Among the casualties: second-seeded Victoria Azarenka (walkover, right knee), men's No. 6 Jo-Wilfried Tsonga (retirement, left knee), John Isner (retirement, left knee) and Steve Darcis (walkover, right shoulder). Darcis was the man who stunned two-time champion Rafael Nadal in the first round Monday.
Also out: 10th-seeded Marin Cilic (walkover, left knee); 2006 quarterfinalist Radek Stepanek (retirement, left hamstring); and Yaroslava Shvedova (walkover, right arm).
The International Tennis Federation said the seven players forced out is believed to be the most in one day at any Grand Slam event in the 45 years of the Open era.
"I would say (it's a) very black day," Cilic said of the spate of injury withdrawals. "The other days, other weeks, there were no pullouts. Everything just happened today."
If that wasn't enough, the tournament lost all those former No. 1 players: Sharapova, Azarenka, Caroline Wozniacki, Ana Ivanovic and Jelena Jankovic among the women, and Federer and Lleyton Hewitt among the men.
With Azarenka and Sharapova gone, the prospect of Serena Williams lifting the women's trophy for a sixth time look even stronger. Williams, who is riding a 32-match winning streak, had already been considered the overwhelming title favorite.
There were a few moments of normality on this crazy day at the All England Club.
Second-seeded Andy Murray advanced easily to the third round with a 6-3, 6-3, 7-5 victory over Yen-hsun Lu of Taiwan on Court 1.
Murray served 11 aces and had 41 winners against only 14 unforced errors for his second consecutive straight-set win. With Nadal and Federer now out of his half of the draw, the U.S. Open champion remains on course in his bid to become the first British man to win Wimbledon in 77 years.
The 20-year-old Larcher de Brito played the match of her life against Sharapova on Court 2 to reach the third round of a Grand Slam for only the second time.
Larcher de Brito held her nerve in the final 10-minute, 18-point game to serve out the match. She saved two break points and finally converted on her fifth match point when Sharapova hit a forehand into the net.
"I can't believe it," Larcher de Brito said. "I just tried to stay calm. I just played so well. I just hung in there. In the last couple of points or games I just gave it my all and went for it."
Sharapova tumbled several times on the grass. Trailing 3-2 in the second set, her right leg gave way behind the baseline and she did the splits.
Sharapova took a medical timeout and complained to the umpire about the grass conditions. She said later she believed she strained a hip muscle.
"I don't think I've ever fallen three times in a match before in my career, so that was a little strange," Sharapova said. "But that's certainly not an excuse.
"I think today I've seen a lot of players fall and take a few hits and a few injuries. So I think that's just part of the game, part of what we have to deal with."
Azarenka, the two-time Australian Open champion, pulled out after hurting her right knee in her opening-round win against Maria Joao Koehler. She withdrew minutes before her second-round match against Flavia Pennetta.
It's only the second time in the Open era that a women's player seeded in the top 2 has conceded a match by walkover at any Grand Slam. The last time it happened was in 1974 at the French Open when second-seeded Nancy Richey pulled out before a match.
Murray's victory came not long after his potential quarterfinal opponent, Tsonga, retired with a left knee injury while trailing Ernests Gulbis 3-6, 6-3, 6-3.
Darcis withdrew a few hours before his scheduled match against Lukasz Kubot of Poland. He said he hurt his right shoulder while diving for a shot in the first set of his win against Nadal.
"Not to go on the court today, it's maybe the biggest disappointing thing I have to do," Darcis said.
The tournament also lost 2002 champion Hewitt, who was ousted by 189th-ranked German qualifier Dustin Brown, 6-4, 6-4, 6-7 (3), 6-2.
The dreadlocked Brown, who switched nationality from Jamaica in 2010, was in tears after beating the Australian.
"I cried like a little girl," said Brown, who has played mainly on the lower-tier challenger circuit in 2013 and had never won a match at Wimbledon until this year.
Isner, the 18th-seeded American, retired during his second-round match against Adrian Mannarino of France after only two games.
The retirement came three years after Isner beat Nicolas Mahut 70-68 in the fifth set at Wimbledon in the longest match in tennis history. Now, Isner is out after one of the shortest matches at Wimbledon.
"I always serve and land on my left leg, like I have done 20 million times playing this game, and this is the first time I just felt this sharp pain," he said.