The Austrian 25-year-old fought back from a set down to triumph 3-6 6-3 7-5 in just over two hours, ending Federer’s chance of a sixth Indian Wells title.
Federer, a 24-time grand slam champion, was the more dominant player in the opening set, racing to a 3-0 lead in 12 minutes and closing out the set in 36 minutes, but Thiem changed the momentum in the second.
Zverev downs Berrettini to clinch second Madrid crown
The Swiss created two break points in the third game but failed to take advantage, while Thiem made sure not to waste his chance in the following game, putting himself 3-1 up and using the confidence to set up a surprise deciding set.
Both players dug deep to take the title fight down to the wire, with Thiem targeting the biggest crown of his career and Federer hoping to clinch his 101st title.
"It is such a pleasure to compete with you, to learn so much from you, and to play with one of the biggest legends of all time," he told the 20-times Grand Slam champion during the trophy presentation ceremony.
"I feel like it's not my right to congratulate you, you have 88 more titles than me, so I'm just hoping you play more and we can have some other big finals," he said with a laugh.
Federer was gracious in defeat after falling in the final in the Southern California desert for a second consecutive year.
"It has been a great week for me even though it didn't work out today," he said.
"What a great week for Dominic. Wonderful playing at the very end and throughout the week. You deserve it."
Thiem really showed his might to save a break point and hold at 4-4, and did not allow a nasty fall and bloody elbow to set him back as he edged the 37-year-old three games later to serve for the title.
The victory means Thiem now moves into the fourth spot in the world rankings.
“It’s unreal,” Thiem told the ATP website.
I had to get used to Roger’s game. In the first set he was playing amazing. It was completely different from my opponents before him. I was struggling to work my way into the match.
“I had to fight to save those break points early in the second set. It was a very good match until the end and I had to fight to serve it out.”
Federer's own serve lost some of its bite in the crucial game in the third set with the score tied at 5-5.
"The game he broke me I didn't serve very well, so I think he took advantage of that fact," said Federer, who was not overly down about the defeat.
"I just got my 100 (career title) in Dubai and I had a good week here," the 37-year-old said.
"Really there is no reason to get down. Plus there is more to look forward to in Miami."
Thiem will also play in Miami, hoping to complete the "Sunshine Double" with a victory there after his Indian Wells win.
Osaka says risk of staging Olympics must be carefully weighed
Zverev puts on Madrid masterclass to down Thiem in semis