Thiem trailed 4-3 and 5-4 in the third set before breaking Federer with a forehand winner to go up 6-5. Thiem closed out the two-hour match Sunday when Federer dumped a forehand into the net for another error.
"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.
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 Milos Raonic, facing only one break point in that match.
Federer and Thiem had split their four previous meetings, but Federer had won both of their hard-court matches without dropping a set.
He cruised through the first set in 36 minutes while getting broken for just the second time during his run to his ninth appearance in the final. But Federer broke back in the next game and served out the set.
"The way he was playing the first set was unreal,'' Thiem said. "I had to get used to it.''
Thiem earned the only break of the second set in the fourth game, going up 3-1. Federer won just two more games in the set.
Both players were on serve in the third set until Thiem collected the only break. Federer tried consecutive drop shots that Thiem retrieved for crosscourt forehand winners before the Austrian hit a winning forehand to lead 6-5.
"He did very well when he got up to the ball, stayed calm, made the shot,'' Federer said.
Federer won just one point on Thiem's serve in the final game.
"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.''
Federer advanced to the final after rival Rafael Nadal withdrew before their semifinal match because of knee pain. Thiem also benefited from a walkover, reaching the semis when Gael Monfils withdrew with an Achilles injury.