The Swiss maestro, after an opening-round bye at the Masters Series claycourt event, was playing his first match since the end of March and only just edged out the big-serving 21-year-old who thumped 21 aces on the Manolo Santana showcourt.
Federer struggled to make any impression on the Raonic serve in the early stages and was broken himself in the ninth game before the Canadian served out to take the first set.
The 16-times Grand Slam singles champion levelled the match with an exquisite drop shot before surviving several scares on his serve in the deciding set and then running away with the tiebreak 7-4 as the young pretender's nerve failed him.
Federer, chasing a fourth title of the year, sealed victory on his first match point with a ripping forehand return that caught the line and set up a meeting with French 14th seed Richard Gasquet.
The 30-year-old Swiss said it was slippery at the back of the court against world number 23 Raonic.
"It was pretty much a big struggle for me tonight but sometimes these kinds of wins can create good things," added Federer.
Raonic's best result before Wednesday was his surprise victory over fourth-ranked Andy Murray at the Barcelona Open last month.
It was his second match against Federer after their clash at the Indian Wells Masters event in March when Raonic, who is ranked 23, also won the first set before losing 6-7 6-2 6-4.
Raonic said he needed to improve his game against the better players.
"I felt I played really well the whole time and I was dictating the match especially on my serve," the Canadian explained.
"It just came down to one or two points but if I want to be in this position against Federer, Nadal, Djokovic ... I'm going to have to improve my returns.
"It's hard not to be disappointed but at the end of the day it's motivating and I know I can get better. With experience, time and a few lucky breaks it will go my way," said Raonic.