LONDON — Dominic Thiem landed one too many punches even for Novak Djokovic to handle.

In a seesawing crowd pleaser that lasted nearly three hours and featured a handful of momentum shifts in the third set, Thiem outlasted Djokovic 6-7 (5), 6-3, 7-6 (5) on Tuesday to become the first player to advance from the group stage at the ATP Finals.

"It was maybe the best match I ever played," said Thiem, a two-time French Open runner-up. "It was a real classic and epic match which will happen from time to time at these big tournaments. It had everything — He was up; I was up. He was playing amazing points. Me, I was playing amazing points."

Djokovic twice rallied from a break down in the third set and led 4-1 in the tiebreaker, but Thiem then won five straight points — taking his total number of winners to 50 for the match — before converting his second match point.

"I don't think I have experienced too many matches like this where my opponent just goes for every single shot," Djokovic said. "I mean, he was unbelievable."

The result leaves Djokovic facing a winner-takes-all match against Roger Federer - in a highly anticipated rematch of this year's Wimbledon final - to decide who will join Thiem in the semifinals.

Federer beat Matteo Berrettini 7-6 (2), 6-3, which means he and Djokovic both have 1-1 records before meeting in their last round-robin match.

Thiem is 2-0 after wins over Federer and Djokovic, and Berrettini is out of contention at 0-2.

The Djokovic-Federer was always going to be the highlight of the group stage at the season-ending tournament - where they have 11 titles between them - but now becomes even more of a blockbuster with a semifinal spot directly at stake.

Federer also has the added incentive of avenging that Wimbledon defeat, when the Swiss star missed two match points before losing 13-12 (3) in the deciding set.

"Actually, it's good for me to play him again, and maybe that all helps to get a chance to get him back," Federer said. "Maybe it took me a few days, couple weeks at most, to get over the Wimbledon loss. We'll find out, but I think it's all flushed away from my side. A lot of water has gone under the bridge since then."

Federer has failed to reach the semifinals only once in his previous 16 appearances at the ATP Finals, in 2008, while Djokovic is trying to equal the Swiss star's record of six titles.

Thiem, for his part, has now beaten Djokovic in four of their last five meetings - although the previous three wins came on clay, his favourite surface.

In the best match of the tournament so far, Djokovic needed to play near flawless tennis in the first set - making just two unforced errors - to hold off the Austrian.

At 65 minutes, the first set alone was longer than Djokovic's entire straight-sets win over Berrettini on Sunday.

But Thiem's aggressive shot-making started paying dividends after that, as he broke Djokovic to take a 2-0 lead in the second set and then again to start the third.

However, Djokovic got back to level at 3-3 and then thought he had earned a match point in the 10th game of the third. With the score at 30-30, Thiem's forehand was initially called wide - before a Hawk-Eye review showed that it had clipped the line.

Thiem held for 5-5 and then broke Djokovic at love - only for the Serb to break back immediately when the Austrian served for the match.

When Djokovic sent a forehand into the net to end it, Thiem fell onto his back on the court before Djokovic gave him a thumbs-up.

"He just played very courageous tennis ... He went for broke," Djokovic said. "I have to put my hat down and congratulate him, because he just played a great match."

The early match wasn't nearly as dramatic.

Federer earned the only break point of the first set at 6-5, but Berrettini saved it with a forehand volley. In the tiebreaker, the Italian double-faulted to hand Federer a 5-2 lead and netted a forehand service return on set point.

Federer broke Berrettini to open the second set but trailed 15-40 in the eighth game, having to save three break points in total before holding with an ace.

He broke again to seal the win.

On Wednesday, Rafael Nadal faces Daniil Medvedev in a rematch of this year's U.S. Open final, before defending champion Alexander Zverev plays Stefanos Tsitsipas.

___

More AP Tennis: https://www.apnews.com/apf-Tennis and https://twitter.com/AP_Sports