The eight-time champion and 20-time grand slam victor last played a Wimbledon match outside Centre Court in 2015.
However, Federer was unable to break his own record after dropping the third set as Anderson embarked on a courageous fightback.
Federer was on the ropes after dropping a comfortable two-set lead, and he even failed to take a match point chance in the third set, but the South African held his nerve to win a captivating contest 2-6 6-7 (5-7) 7-5 6-4 13-11.
While Federer started out strong, besting Anderson 6-2 and 7-6 in the first few sets, his dominance became less complete as play went on. Anderson will take on the victor of Milos Raonic and American John Isner.
It was his first win against Federer at the fifth attempt and means he is the first player representing South Africa to reach the semifinal at the All England Club since Kevin Curren in 1983. Every set won by Anderson required an extra game to determine the victor. I think that was the key at the end.More news: England vs. Croatia semifinal odds, lines, expert picks, and best insider predictions
He saved a match point at 4-5 when Federer missed a forehand, then broke to serve for the third set.
Anderson had Federer down 0-30 twice on serve in the early stages of the decider but the top seed's survival instincts kicked in and with the advantage of serving first it seemed inevitable that his opponent would crack eventually.
It sounds cliché but it is one of the most important things on a tennis court.
During his postmatch media duties, Anderson, 32, reiterated he was playing some the best tennis of his career but might have to find another level when facing defending champion Federer, likely on Centre Court, on Wednesday.
Federer won the first meeting 6-4, 6-4 at the Paris Masters indoor hard-court event in 2013; he won 7-5, 6-1 at Indian Wells in 2014; 6-3, 7-5 on the clay in Rome in 2015 and 6-1, 6-1 in Cincinnati later that year.More news: Turkey's Erdogan sworn in with new powers, names son-in-law finance minister
"I feel like a lot of aspects of my game can give him a lot of trouble", Anderson said.
Djokovic had to play that fourth round on Tuesday and quarter-final on Wednesday when he was forced to retire against Tomas Berdych with an elbow injury.
However, it was Federer who was stunningly the first to falter and Anderson took advantage to book a semifinal clash with either John Isner or Milos Raonic.
It was an absorbing battle full of surprise, suspense and swings of momentum - unquestionably the match of the men's singles tournament so far.
Ramos then hit Djokovic with a time violation warning for taking too long to serve in the seventh game of the fourth set.More news: Zsa Zsa, the world's ugliest dog, has died