Matteo Berrettini in action against Dominic Thiem at the ATP Finals
Matteo Berrettini in action against Dominic Thiem at the ATP Finals AFP / Ben STANSALL

Roger Federer produced a near-flawless performance as he avenged his Wimbledon defeat by Novak Djokovic and qualified for the last four of the ATP Finals with a 6-4, 6-3 victory on Thursday.

The Swiss started the tournament with a chastening straight-sets defeat to Dominic Thiem but found his best form when it mattered.

Defeat for the second seed spells the end of his bid to overtake Rafael Nadal and finish as year-end number one.

Roared on by a raucous packed house at London's O2 Arena, six-time champion Federer looked in the groove from the start, cranking up the pressure on Djokovic's serve and dropping just three points on his own serve in the first set.

The Serbian upped his game at the start of the second set but Federer, 38, saved the one break point he faced and broke twice to canter to victory.

The third seed, making his 17th appearance at the ATP Finals, is into his 16th semi-final at the year-end event.

Djokovic needed to win the title to have a chance at knocking Nadal off the top spot, but now the Spaniard is guaranteed to finish the year as the top-ranked player for the fifth time, tying him with Federer, Djokovic and American Jimmy Connors.

Roger Federer in action against Novak Djokovic at the ATP Finals
Roger Federer in action against Novak Djokovic at the ATP Finals AFP / Ben STANSALL

"Great atmosphere, great opponent," said Federer, who hit 23 winners and made just five unforced errors. "It was definitely incredibly special. I enjoyed it from the beginning.

"I played incredible and I knew I had to because that's what Novak does. It was definitely magical."

Speaking about what was different from the Wimbledon final, where he squandered two championship points on his own serve, he said: "I won match point I guess.

"It was so close at Wimbledon. It was a privilege to play that match, so many ups and downs. I couldn't be more happy right now."

Spain's Rafael Nadal is guaranteed to finish the year as the world number one
Spain's Rafael Nadal is guaranteed to finish the year as the world number one AFP / Adrian DENNIS

Federer finishes second in Group Bjorn Borg, behind Thiem, who also beat Djokovic earlier this week. The Swiss will face the Group Andre Agassi winner on Saturday.

Djokovic looked nervy at the start of the winner-takes-all contest, double-faulting twice in the third game, in which he was broken to love.

As cries of "Let's go Roger, let's go" rang around the cavernous stadium, Federer was dead-eyed on his serve, hitting eight aces, including a second-serve ace, in the first set.

Federer's service level dipped in the second set and 32-year-old Djokovic earned his first break point of the match in the fourth game, which the Swiss saved.

In the next game Djokovic slipped to 15-40 and sailed a forehand long to give Federer his second break of the match. The Swiss broke once more to close out victory.

Djokovic had won his past five meetings with Federer, including their epic five-set battle in the final at Wimbledon in July.

"He was the better player in all aspects and absolutely deserved to win," said Djokovic. "He served great, moved well, returned my serve very well.... He did everything right."

In Thursday's early match in Group Bjorn Borg, which was a dead rubber, eighth seed Matteo Berrettini beat Thiem 7-6 (7/3), 6-3.

Fifth seed Thiem did not hit the heights he reached during his three-set win against Djokovic, notching just 12 winners compared with 50 against the Serbian.

Stefanos Tsitsipas has already qualified for the semi-finals from Group Andre Agassi, leaving Nadal, defending champion Alexander Zverev and Daniil Medvedev to scrap it out for the other spot on Friday.