Updated

Matt Cain tossed 7 1/3 innings of one-run ball to earn his first win of the year, and Hunter Pence smacked two doubles and drove in four in the San Francisco Giants' 4-3 win over the Los Angeles Dodgers to complete a three-game sweep.

Cain (1-2), who was a 16-game winner last year, fanned four and surrendered five hits while walking three for San Francisco, which has won six straight on the heels of a five-game skid.

"He got us into the eighth inning, and as usual our boys are entertaining and made it close again. But I was happy for Matt the way he threw the ball," Giants manager Bruce Bochy said.

Cain had 6.79 earned run average entering Sunday's tilt.

Hyun-Jin Ryu (3-2) allowed four runs on eight hits and two walks over six frames for the Dodgers, who have dropped four contests in a row.

"I feel better about our club walking out of here right now than I did walking in," Dodgers manager Don Mattingly said. "We played them tough the whole series."

San Francisco built a 4-0 lead after seven, but a three-run eighth got LA back into the game.

With the bags full and two away in the frame, pinch-hitter Adrian Gonzalez rolled a two-run single back through the box off Jeremy Affeldt to cut the deficit in half. Dee Gordon then reached on an infield single to bring home another run to make it 4-3.

But Jean Machi came on and induced an inning-ending groundout off the bat of pinch-hitter Jerry Hairston. Sergio Romo worked a 1-2-3 ninth to notch his 12th save of the year.

The Giants jumped ahead after plating a run in the first, another in the third and two more in the fifth.

Three straight base hits by Andres Torres, Marco Scutaro and Pablo Sandoval loaded the bases before Torres was thrown out at home on Buster Posey's groundout. But Pence's groundout to shortstop brought home Scutaro for the game's first run in the opening inning.

Scutaro hit a one-out single and Posey's walk two batters later put runners on first and second in the third for Pence, who ripped a double to left to bring home Scutaro.

In the fifth, Sandoval worked a two-out walk and Posey singled to center before Pence poked a Tobago to right for a 4-0 San Francisco advantage.

Game Notes

San Francisco recorded three one-run wins to earn the sweep ... Los Angeles was 2-for-12 with runners in scoring position and left nine men on base ... Pence has seven doubles this year.