The eight-time champion was two sets up and had a match point in the third, but nearly three hours later he trudged off on the end of a seismic 2-6 6-7 (5/7) 7-5 6-4 13-11 upset.
Defending champion Federer hadn't been broken since the opening set of his 2017 semifinal victory over Tomas Berdych.
And with Federer dumped out of Wimbledon, the competition has opened up and paved the way for his arch rival - Rafa Nadal - to claim his third title at the All England Club.
The Spaniard finished with 67 winners to 34 unforced errors as he displayed his credentials as he bids to lift a third singles title at the All England Club. But that turned out to be the least of his worries. In the penultimate game, Federer hit a shot that went close to the line, the crowd gasped, but Anderson was able to get the ball back in play.
Federer is stuck on 20 tennis majors - an astonishing feat, of that there is no doubt.
The Swiss great won the first two sets and had a match point at 5-4 in the third, before Anderson hit back to level at 6-2, 7-6 (5), 5-7, 4-6.
Anderson traded with a defensive Federer as they rallied to enter sudden death. Federer ended up with more points won than Anderson in this match, but ultimately the difference came down to two unforced errors.
Nadal demolished the giant Argentine for the loss of just seven games in the French Open semi-finals last month.
Serving with a deceptive languor, Federer was secure and invulnerable, while Anderson struggled to hold his own, handing Federer a second break and a 5-2 lead.
But this time Federer cracked, serving his first double fault at 11-11 to give Anderson the crucial break that ushered the Swiss legend to the exit door.
England boss Gareth Southgate
For all of the players and all of the staff, we got back to St Petersburg at 6.30am after the game, so emotionally it has been a very hard few days.
Serena Williams reaches Wimbledon final months after returning from maternity leave
Serena Williams longs to tell her daughter how she bound her longtime label, champion, with her newer one, mother. Goerges, playing her first Slam semifinal, sent a forehand flying wide off the sidelines to concede serve.
Here’s the Paul Manafort mugshot we’ve all been waiting for
On Tuesday, a federal judge ordered Manafort be moved to a closer detention facility in Alexandria, Virginia. Paul Manafort's lawyers are concerned about where their client is being held as he awaits trial.
Nishikori was bidding to become the first Japanese man to make the semi-finals in 85 years.
"I just kept telling myself to keep believing and that today it would be my day, that's the mindset you need against someone like Roger". I felt I was really in the flow of the match. I said today is going to be my day.
"That was something I learnt from 2016 where I constantly tried to get back into competition too quickly".
Djokovic had played just one of his first four matches on Centre but requested to be moved for his meeting with Japan's Nishikori.
Rafael Nadal, the reigning champion, was forced out of the 2014 U.S. Open with an injury, clearing Federer's side of the draw for what appeared to be a cakewalk to an inevitable final against Novak Djokovic.
Later in the match, Djokovic was clearly frustrated and complained of "double standards" when Nishikori was not punished for a racket throw.
But I changed that phrase and said 'What's happened in the past is in the past. "I think that was the key at the end".
"At that point, I wasn't thinking about losing", Federer said. Kei did the same in the fourth set but wasn't warned. I think that's not fair.
"I was seeing their battle between five-or-six-all to about nine-all and he's constantly under pressure, Kevin, and he was coming up with some big serves and bold tennis".
He had been yearning for a Centre Court date, after playing as many games on Court Two as the main show court so far this tournament.
"That was very important", he added. In tough times you can sort of rely on "hey, I feel like I'm a better tennis player, I'm going to get through". So I'm all right.