The 10th seed from Thornhill, Ont., edged the Spaniard 7-6 (1), 3-6, 6-3 on Wednesday.
Raonic is making a late-season run at qualifying for the ATP Finals, as well, reaching back-to-back finals in Bangkok and Tokyo the last two weeks. He won the title in Thailand but lost to Juan Martin del Potro in Japan.
He's 10th in the standings for the ATP Finals, trailing Richard Gasquet of France by less than 300 points. Raonic will play No. 8 Stanislas Wawrinka in the next round after the Swiss defeated South African Kevin Anderson 6-4, 3-6, 7-6 (5).
Fellow Canadian Vasek Pospisil of Vernon, B.C., was eliminated following a 7-5, 7-6 (4) loss to Gael Monfils of France.
Meanwhile, Novak Djokovic survived a scare when he landed awkwardly on his right foot and recovered to beat Spain's Marcel Granollers 6-2, 6-0.
Roger Federer and Rafael Nadal had fewer problems in their second-round matches. Federer rebounded from an early break to defeat Andreas Seppi of Italy 6-4, 6-3, while Nadal, who reclaimed the No. 1 ranking this week, dispatched Alexandr Dolgopolov of Ukraine 6-3, 6-2.
Djokovic hit a running backhand in the first game of the second set and then leaped in the air, wincing in pain. He called for a medical timeout after the game to have his foot wrapped and appeared slightly tentative after returning to the court, not bothering to chase after a couple of shots.
The Serb dominated the rest of the match, however, closing it out in 70 minutes to set up a third-round match with Italian Fabio Fognini, who advanced when Tommy Robredo retired with a wrist injury while trailing 7-6 (3), 2-1.
Djokovic said the problem wasn't serious, without elaborating about where specifically he felt the pain.
"It was just an awkward movement," he said. "I was running and I made a quick movement that caused a sharp pain. It's all good now," he added succinctly when questioned further.
Federer, the fifth seed, had more unforced errors (29) than winners (19) in his first match since a fourth-round loss at the U.S. Open, but he made quick work of the 22nd-ranked Seppi.
"Even though it didn't feel like I've been away for a long time, you kind of know it," Federer said. "Instead of just thinking point for point, what do I want to do, your mind starts to wander — not in a bad way, but it's just not that natural yet."
Federer next plays Monfils, with a possible quarter-final with Djokovic looming.
Two other top players, Tomas Berdych and Juan Martin del Potro, also came through with victories despite struggling with injury and illness.
Fourth-seeded Berdych rebounded from a back injury sustained in the semifinals of the China Open last week to defeat Feliciano Lopez 7-6 (5), 6-2. He said after his last match he could barely stand or walk when he retired against Nadal.
"I wouldn't say (my back is) 100 per cent, but I decided I was competitive enough to go on court and try to play," he said. "Looks like it was a smart decision."
Del Potro, meanwhile, overcame a fever and 50 winners from big-hitting German Philipp Kohlschreiber to advance 3-6, 6-3, 7-6 (4).
After the match, del Potro said he started feeling sick on Tuesday night and relied on his serve to win the match since running was difficult.
The Argentine withdrew from the doubles to rest for his next match against 11th-seeded Tommy Haas, a 6-4, 6-4 winner over German compatriot Daniel Brands.
Like Raonic, del Potro and Berdych are also fighting for spots in the ATP Finals, a task made slightly easier by the withdrawal on Wednesday of Andy Murray, who is recovering from back surgery.
Del Potro and Berdych would qualify for the eight-man field by making the final in Shanghai.
Federer needs to do a bit more work — he's in seventh place in the standings, with a number of players within striking distance.
Two of them moved into the next round on Wednesday: No. 7 seed Jo-Wilfried Tsonga and Wawrinka.
American John Isner, an outside shot for London, however, was ousted by Argentine Carlos Berlocq 6-4, 7-6 (4).
Note to readers: This is a corrected story. An earlier version reported that Pospisil lost to Gasquet.Suggest a correction