The top-seeded Swiss beat his Canadian opponent for the 11th time in 14 meetings while winning a first Stuttgart title.
Federer, who will be chasing a ninth Wimbledon triumph next month, finally came good on the German grass on his third attempt after losing a semi-final in 2016 to Dominic Thiem and falling in the first round here a year ago to good friend Tommy Haas.
Roger. Wins. Again. @rogerfederer captures his third 🏆 of 2018, No. 18 on grass, and No. 98 overall with a 6-4, 7-6 victory against Milos Raonic at the @MercedesCup. How many will he win in his career? 🤔@Infosys #ATP Scores & Stats ➡️ https://t.co/QAotlqBCJ7 pic.twitter.com/BjqInSWb2v— ATP World Tour (@ATPWorldTour) June 17, 2018
The 36-year-old will regain the world number one ranking on Monday and is playing next week as top seed in Halle.
"It's a great comeback for me," Federer said after completing his 78-minute win.
"I'm so happy to win this tournament in my third attempt. We'll see if being number one again will probably give me a boost."
Federer made a return to the top ATP ranking for a sixth time through his semi-final victory on Saturday over Australian Nick Kyrgios.
He takes the honour back form Rafael Nadal for the second time this season and continues the duel on court between the two modern icons.
Federer kept tight control on the match as he claimed his third title of the season after the Australian Open and Rotterdam and now owns 28 grass trophies including eight from Wimbledon
"I think I played very well not having played for a while," Federer said as he competed this week for the first time since mid-March after skipping the clay season.
"Maybe I was a bit better on the big points."
The Swiss broke twice in the first set to earn it after 32 minutes when his opponent returned long over the baseline.
The second set stayed on serve into the tiebreaker.
A double-fault from the Canadian followed by a return winner from the Swiss set up three match points, with Federer needing only his first as Raonic netted a return.
Federer improved to 21-2 on the season after playing in the 24th grass final of his career.
The 35th-ranked Raonic, a victim of various injuries over recent seasons, was playing a final for the first time in his last 16 tournaments dating to Istanbul last year.