Federer triumphed 7-5, 6-2 in their group stage clash at the O2 Arena, winning the key points at crucial moments to hand the Serb his first defeat on an indoor court in 39 matches, ending a winning run stretching back to 2012.
World number three Federer became the first man to reach the last four at the season-ending tournament, disputed between the top eight players on a slow-playing hard court.
Meanwhile, defending champion Djokovic, on a 23-match winning streak, suffered his first defeat since losing to Federer in the Cincinnati final on August 23rd.
Earlier in the same group of four, Japan's Kei Nishikori gave his hopes of reaching the semis a major boost with a 7-5, 3-6, 6-3 victory over Tomas Berdych of the Czech Republic.
Federer and Djokovic were locked at 21-21 in career matches between them going into the match.
Federer converts chances
Federer won a tight first set, having won the toss and chosing to serve.
Djokovic incorrectly challenged a line call on the first point of the match and earned a break point in the game but Federer saw it off.
The set went with serve and the Swiss took the 11th game to love to put the pressure on Djokovic, serving at 5-6 down. Federer had a first set point at 30-40 up, but hit the net in a rally.
On a second deuce, Federer converted his second set point with a delicate backhand drop over the net that had the six-time champion's numerous fans on their feet celebrating.
Federer led 2-1 in the second set after a quick exchange of breaks and broke for a 4-2 lead with a shot that clipped the net, deceiving Djokovic.
He took the next game to love, to leave the defending champion with a mountain to climb.
Federer earned himself a match point with a tremendous backhand that had supporters punching the air.
However, Djokovic triumphed at the end of a long rally to stay clinging in the contest.
On Federer's second match point, Djokovic shot wide and challenged, but the ball was correctly called out as Federer celebrated victory.
Nishikori survives stumble
Earlier, eighth-ranked Nishikori survived a second set wobble but recovered to beat world number six Berdych 7-5, 3-6, 6-3 thanks to what he admitted was a "lucky" shot.
Both players lost their Finals group stage opener — Nishikori to Djokovic and Berdych to Federer.
A second defeat for Berdych therefore left his semifinal hopes hanging by a thread.
Nishikori, the youngest player in the Finals at 25, made the semifinals last year in his debut at the O2 Arena.
Berdych was 2-0 down in the second set but turned it around, winning five games in a row.
In the third and deciding set, Nishikori saved a break point at 3-3, the key moment in the match.
Asia's top player then broke Berdych and closed out the two hours and 23 minutes-long match with a game to love.
Nishikori said he was looking forward to Thursday's clash with Federer, the youngest player in the tournament facing the oldest.
"It's going to be a really big challenge for me to play Roger. It's always exciting," he said.
"He doesn't look really his age, 34. He's still improving himself, playing great tennis even though he's 34. He doesn't look old for me. It's a great honour to play Roger."
Even if Berdych beats Djokovic in his final group match on Thursday, his chances of making the last four would still be slim — something that is not lost on the 30-year-old.
"Just try to have a good one for the last match of the season," said Berdych.