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.
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.
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.
Federer's shocking 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 managed only one break of serve 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.
''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.''
(For more sports news and updates, follow Ahram Online Sports on Twitter at @AO Sports and on Facebook at AhramOnlineSports)