“It's fantastic to come here, face the pressure and be able to come through at the end,” the World number two told Swedish TV.
Mayer, without a title to his name, had a mountain to climb but came out strongly, attacking at every opportunity and displaying a fine touch around the net.
He broke serve in the first set, only for Federer to break back immediately.
The Swiss then held his own serve to love before breaking again to take the set.
Mayer wrong-footed Federer with some classy shots but a service break in the sixth game of the second set put the 16-times Grand Slam winner in the driving seat.
Second seed Victoria Azarenka beat Russian Maria Kirilenko 6-3, 6-4 in a see-saw Kremlin Cup final on Sunday to win her second title of the year.
Azarenka broke the sixth seed three times in the opening set. It was the first set conceded by Kirilenko all week, with the Muscovite dropping only nine games in her previous four matches here.
Kirilenko returned the favour by breaking the more powerful Belarussian twice at the start of the second to take a 4-0 lead but the world number 10 battled back to reel off six straight games and add the crown to her US victory in Stanford.
Unseeded Serbian Viktor Troicki defeated fourth-seeded Cypriot Marcos Baghdatis 6-4, 6-3 in the men's final.
Rising Indian star Somdev Devvarman inched towards the main draw of the ATP Bank Austria Tennis Trophy after beating Belgium’s Ruben Bemelmans in the first round of the qualifiers in Vienna. The Indian won 6-3, 7-6 (11-9). He will next meet Romania’s Marius Copil, who beat Franco Skugor of Croatia 6-3, 7-6 (7-2).