Stunned by the young Canadian's power serving in the opening set, the Swiss maestro delivered a master class in the next two, sealing victory in just over two hours when the towering Raonic netted a forehand.
"For me it was a difficult match," Federer said after receiving rapturous applause from the near-capacity crowd at the Indian Wells Tennis Garden.
"He's probably seen me play a whole lot of times on TV which helps him out a bit. But I really did well. I hung in there and played a great second set.
"The third set was close and I'm happy to be moving on in the tournament," added the Swiss, who had swept past American Denis Kudla 6-4 6-1 in the previous round while shaking off a flu bug and slight temperature.
On a cool evening in the California desert, the opening set went with serve and into a tie-break that Raonic won 7-4 when Federer hit a forehand service return long.
However the Swiss immediately broke the Canadian in the next game, firing a pinpoint forehand winner down the line to lead 1-0. He again broke in the fifth when he blasted a forehand past Raonic who was stranded at the net to go 4-1 up.
With the crowd eagerly watching the action on the showpiece stadium court, world number three Federer comfortably served out to level the match at one-set all.
The final set went with serve until the seventh game when Federer coolly set up a break point with a whipped forehand winner and immediately took advantage when he passed Raonic with a crunching backhand winner down the line to lead 3-4.
Federer, who clinched three consecutive titles at Indian Wells from 2004, served out to seal the win, wrapping up victory on his first match point after a crunching forehand winner down the line had put him 40-15 up.
"I was getting to the ball often in the beginning but just not making the plays," said the Swiss, who has won 35 of his 37 matches since last year's US Open. "That was hurting me.
"Then I was able to improve, and once I got that going maybe his serving also just dropped a tiny bit and gave me more opportunities on his second serves."
Federer paid tribute to emerging talent Raonic, who has already recorded five victories over top-10 players.
"He serves really well and hes going to be for sure a future top-10 player, and a whole lot better," the 16-times Grand Slam champion said. "He did very well tonight under pressure."
Raonic, whose fastest serve was clocked at 142 mph (228.52 kph), enjoyed the experience of playing Federer.
"There were a few things I could have done better, but I was there competing against someone people say is the greatest player of all time," he said.
"So that's definitely a positive, and it's something to build off of.
"I did a few things well in the first set. After that, he just picked it up on the return. I don't think I served as well after, either. It was just a bit of a mix on that ... and he then just put pressure on me."