It is the first time since 2015 that the eight-time champion will play away from Centre Court.
The first set took just 16 minutes, and Federer said: "I was also surprised it was that fast".
Federer hasn't dropped serve at Wimbledon so far but is wary of the challenge posed by Anderson whose big serving style is flourishing on courts cooked by last week's heatwave. But that turned out to be the least of his worries.
Anderson's comeback started here, however, and the South African found his distance with the serve to win back-to-back sets and level.
Federer responded, quickly breaking back before taking the set in a tie-break.
It was a surprise result that now sends the eighth-seeded Anderson to the semifinals at Wimbledon, his first time making it that far in the storied competition that Federer has won eight times, including past year.
South Africa's Kevin Anderson celebrates during the fourth round match against France's Gael Monfils.
The final set lasted 90 minutes, and it was captivating stuff. Anderson survived match point and then broke with a backhand down the line, going on to take the match to a fourth as Federer spurned three break points, an ace ending his run of 34 consecutive sets at Wimbledon.
It was Federer who faltered at 11-11, double-faulting to hand Anderson a break point which he converted when the defending champion's tired forehand smacked the net.
Nadal reached the quarterfinals of Wimbledon for the first time since 2011 after defeating Jiri Vesely in a 6-3, 6-3, 7-5 victory on Monday.
Since he exited Wimbledon at the first-round stage a year ago, Anderson has enjoyed a solid return, making four finals, including his deep run at the US Open where he finished runner-up to Rafael Nadal.
"Down two sets to love I tried my best to keep fighting".
"Beating Roger Federer at Wimbledon is definitely one I'm going to remember". That's the mindset you need against someone like Roger.
The only player with a superior head-to-head record over both grand slam giants, Djokovic has warned he's finally approaching his own brilliant best after an utterly unexpected two-year major title drought.
"But today we savour this man's heroic character and tennis". I still felt like all he needed to do was give me a few second serves, finally pick the right sides again, things could change.
Djokovic, seeded 12th, incurred a warning for throwing his racket and was clearly frustrated, complaining of "double standards" when Nishikori was not punished for a racket throw.