The eighth-seeded Anderson trailed two sets to none and faced match point in the third set before staving off elimination to win the set 7-5.
Certainly, few expected the match to be a 4-hour marathon after Federer took the first set in 26 minutes.
It was Federer's earliest departure from Wimbledon since his shock second-round defeat against Sergiy Stakhovsky in 2013.
Anderson will take on the victor of the quarter-final clash between former Wimbledon finalist Milos Raonic and ninth-ranked John Isner.
The three-time champion spoke to Serbian media after his straight sets victory over Khachanov and revealed he has put in a request to play on Centre Court, having played there only once in this year's tournament.
The 32-year-old made his maiden Grand Slam final appearance as last year's US Open, losing to Rafael Nadal.
"He claims that he didn't see what Nishikori has done, but apparently he always sees what I do", Djokovic said afterward, "something that I don't think is fair".
South Africa's Kevin Anderson has beaten defending Wimbledon champion Roger Federer in five gruelling sets.
The battle between the two big-servers will start on Court 1 after the first quarter-final between Federer and Anderson.
Novak Djokovic of Serbia returns the ball to Karen Khachanov of Russian Federation during their men's singles match on the seventh day at the Wimbledon Tennis Championships in London, Monday July 9, 2018.
Federer had a match point in the third set at 4-5, 30-40 but failed to take it, before Anderson broke in the following game to earn the opportunity to serve out the set.
Kevin Anderson, left, rallied from two sets down to deny Roger Federer's bid to win his ninth Wimbledon title this year.
And it was Federer who blinked, double-faulting to face a break point at 11-all, then slapping a forehand into the net to cede the key break.
As it stands, it looks like it very well could be a repeat of the classic 2008 Wimbledon final where Nadal and Federer face each other in the final and rekindle memories of what is arguably the greatest tennis match of all time.
It is the second time that 20-times Grand Slam champion Federer has lost at Wimbledon from two sets ahead, suffering the same fate against Jo-Wilfried Tsonga in the 2011 quarter-finals. When I'm doing that, I think I'm a very unsafe player.
Nishikori, playing in his first quarterfinal at the All England Club, was bidding to become the first Japanese man to make the Wimbledon semifinals in 85 years.
Even by Federer's stratospheric standards, the first set was a little absurd.