Leadoff hitter Andres Torres had three hits for San Francisco while Marco Scutaro had two hits and scored twice. The Giants extended their season-high winning streak to six games and defeated Dodgers rookie Hyun-Jin Ryu for the second time this year.
Cain, who pitched six shutout innings against Los Angeles on opening day, gave up one run and five hits in 7 1-3 innings and allowed only one runner past second base while ending the longest winless drought of his career.
A.J. Ellis had two hits for the Dodgers.
The Giants won the first two games of the series on game-ending home runs but didn't need any late dramatics this time, though the Dodgers made it close with three runs in the eighth.
San Francisco took an early 2-0 lead off Ryu and made it hold up behind Cain (1-2) and five relievers.
Cain, who struck out four and walked three, had not won since Sept. 26, 2012. He left to a standing ovation from the AT&T Park crowd after issuing a one-out walk in the eighth.
It's also the first win by any San Francisco starting pitcher since April 21 - an 11-game stretch.
Sergio Romo worked the ninth for his 12th save, tied for most in the majors.
The Dodgers couldn't get out of AT&T Park fast enough. They were the only visiting NL team with a winning record at the Giants waterfront ballpark before the series but are now 60-60 since the stadium opened in 2000.
It didn't help that manager Don Mattingly had to scratch first baseman Adrian Gonzalez from the lineup with a sore neck less than an hour before first pitch. Gonzalez, who was also scratched from the first two games of the series, delivered a pinch hit two-run single in the eighth before leaving for a pinch runner.
Ryu (3-2) has been one of the most consistent pitchers for Los Angeles except against San Francisco. He gave up 10 hits and three runs while taking the loss in his majors debut on April 2, then allowed eight hits and four runs over six innings in the rematch.
The Giants opened with three straight singles off Ryu in the first but the Dodgers right-hander got out of it allowing only one run. Scutaro scored on Pence's fielder's choice grounder after Buster Posey hit into a forceout at home. Ryu then retired Francisco Peguero on a fly ball to end the inning.
Pence, who was hitting just .211 with runners in scoring position, doubled Scutaro home in the third then hit a two-run double in the fifth to drive in Pablo Sandoval and Posey.
After Cain walked Matt Kemp in the eighth, Ellis singled off reliever George Kontos and Juan Uribe walked. Gonzalez followed with a two-run single to center and Uribe scored on an infield hit by Dee Gordon to make it 4-3. Jean Machi got pinch hitter Jerry Hairston to ground out with the tying run at second base to end the inning.
Notes: San Francisco CF Angel Pagan did not play because of a strained hamstring and is questionable for Monday's series opener against Philadelphia. ... Opponents are batting just .181 against Giants LHP Madison Bumgarner. Bumgarner (3-0) pitches the first game against the Phillies. ... Chris Capuano (0-1) is scheduled to come off the disabled list and start for Los Angeles on Monday against Colorado.