Fred Lewis finished a home run shy of the cycle and Nate Schierholtz hit a solo shot in the fourth, snapping the Giants' seven-game homerless streak.
Cain (12-2) gave up one run and struck out five as he moved to 37-6 all-time when the Giants score three or more runs for him.
Jeremy Affeldt pitched a perfect eighth -- his 27th straight scoreless inning -- and Brian Wilson got Troy Tulowitzki to hit into a game-ending double play in the ninth for his 25th save.
Although Cain struggled with his command at times, issuing four walks, he didn't allow his first hit until the fifth when Ian Stewart lined a two-out hopper through the right side of the infield.
Cain joins Colorado's Jason Marquis as the only 12-game winners this season. Marquis was scheduled to pitch Friday, but was bumped due to a bothersome blister on his right middle finger.
Jason Hammel (5-5) filled in, giving up three runs and seven hits in six innings.
The Rockies had a two-out rally going in the sixth after Ryan Spilborghs doubled and Todd Helton walked. Garrett Atkins laced an RBI double to left, but Cain struck out Brad Hawpe to limit the damage to one run.
Lewis finds hitting at Coors Field to his liking, increasing his career batting average to .365 with a 3-for-3 night. He scored two of San Francisco's runs.
The left-handed hitting Lewis singled in the first, doubled in the fourth and tripled in the sixth. In his final at-bat in the eighth, he was walked on five pitches by southpaw Franklin Morales.
Lewis hit for the cycle on May 13, 2007, at Coors Field.
The Giants entered the game as the only team without a homer following the All-Star break. Schierholtz ended that dubious distinction, connecting on a 93-mph fastball from Hammel. It was his fourth of the season.
Rafael Betancourt made his Rockies debut in the eighth inning, setting the Giants down in order. Betancourt was acquired Thursday from Cleveland and added to the roster before the game.
NOTES: Giants OF Aaron Rowand (bruised right forearm) and INF Edgar Renteria (right elbow) were given another day off to rest. They're expected to be available Saturday night. ... LHP Randy Johnson (strained shoulder) is going to have an MRI on Monday to see how much progress he's made. "We're still being optimistic we'll get good news Monday and he'll start his rehab," Giants manager Bruce Bochy said before the game. ... Marquis is scheduled to start Tuesday night in New York against the Mets.