Dominic Thiem edged error-prone Roger Federer 3-6, 6-3, 7-5 to win the men's final, denying Federer a record sixth title in the desert.

Thiem trailed 4-3 and 5-4 in the third set before breaking Federer with a forehand winner to go up 6-5. Thiem served out the two-hour match Sunday that ended with another error from Federer, a forehand dumped into the net.

"It just feels unreal what happened," Thiem said. "He's such a legend."

Federer was in the final for the third straight year and lost for the second year in a row. He was beaten in a third-set tiebreaker by Juan Martin del Potro last year. Federer won his 100th career title in Dubai recently.

"Just came up against somebody who was on the day a bit better when it really mattered," Federer said. "I have been in these positions so many times that I get over it very quickly."

Thiem had lost in his previous two ATP Masters 1000 finals. But the 25-year-old Austrian's solid serve held up against Federer as it had throughout the tournament.

Thiem was broken just four times out of 61 service games in the tournament. He didn't lose serve during his semifinal win over Canada's Milos Raonic, facing only one break point in that match.