SAN FRANCISCO -- A.J. Ellis didn't have to do much guessing when he came up to face Giants closer Sergio Romo with the bases loaded in the ninth. Having been a part of this rivalry his entire big league career, Ellis knew exactly what to expect.
He was right.
"It's a great thing about the Dodgers and Giants, there's no secrets," Ellis said. "With Sergio right there, you know what his bread and butter is. You know he's trying to throw a slider. Fortunately for me and for the team I found a ball that I could hit."
With the win, the Dodgers moved into sole possession of second place in the NL West heading into a three-game series with division-leading Arizona.
Ellis' game-winning hit capped a heated series between these two longtime rivals. Players from both sides jawed at one another throughout the three games, and Romo even mocked Dodgers slugger Hanley Ramirez after securing a save on Saturday.
Romo didn't have anything to say after Sunday's game. He quickly dressed then brushed past waiting reporters in the Giants clubhouse without speaking.
The Dodgers didn't seem too concerned about it. Their fourth consecutive series win moved them ahead of the Giants and Rockies, 4½ games behind the Diamondbacks.
"We need to win games like that," said Kershaw, who improved to 11-4 lifetime against the Giants. "We've shown we can swing the bats and blow some guys out recently. Tight games like that we're going to need a clutch hit and A.J. came through today. That was huge."
Kershaw (8-5), a candidate to start the All-Star Game for the National League, wasn't as crisp as he'd been in his previous two starts but still only allowed one run over eight innings. He struck out three and overcame hitting his first batter of the season.
Kenley Jansen worked the ninth for his ninth save.
"He just imposes his will on not just these guys but everybody," Ellis said. "He was attacking the strike zone and keeping them off balance."
Ramirez doubled and tripled to extend his career-high hitting streak to 18 games for Los Angeles while rookie Yasiel Puig added two hits, including a single that started the rally in the ninth.
"There's no question it's a tough loss," Giants manager Bruce Bochy said. "These guys are resilient and handling this as well as they can."
Puig singled off Romo (3-4) to open the ninth. Adrian Gonzalez followed with a sharp single that first baseman Buster Posey initially knocked down. Posey tried flipping the ball to shortstop Joaquin Arias covering second but threw the ball into left field for an error.
When San Francisco third baseman Pablo Sandoval raced to retrieve the ball, Puig alertly advanced to third. Ramirez was retired on a comebacker, Andre Ethier was intentionally walked to load the bases and Juan Uribe struck out before Ellis lined a two-out double into left-center.
That secured Kershaw's third win over the Giants this season while keeping the NL's ERA leader in line for a possible start in the All-Star Game in New York on July 16. Bochy, who will lead the National League, will make the decision.
"I had pretty good command other than (walking) the pitcher," said Kershaw, who has a 0.79 ERA in 10 games at the Giants waterfront stadium. "It's a great park to pitch in. I just like it here."
Gaudin, who had been out since getting hit in the right elbow by a line drive from Miami's Derek Dietrich on June 20, was just as sharp as the Dodgers' three-time All-Star.
He retired 12 of 13 during one stretch and worked out of a two-on, two-out jam in the seventh by striking out Uribe before departing. Gaudin finished with nine strikeouts -- his most since June 28, 2009 -- and one intentional walk.
Ramirez tripled leading off the second against Gaudin and Ethier followed with a bloop single to drive in the Dodgers first run.
Puig, who struck out twice in his first three at-bats after fanning four times on Saturday, made a diving catch on a fly ball by Quiroz in the eighth.
LHP Michael Kickham was optioned to Triple-A Fresno before the game to make room for Gaudin. ... OF Matt Kemp was held out of the lineup to rest his ailing shoulder but is expected back in the Dodgers lineup on Monday. ... Los Angeles manager Don Mattingly said newly acquired Ricky Nolasco will replace LHP Chris Capuano in the Dodgers rotation. ... Giants RHP Santiago Casilla will pitch at least twice more in the minors before rejoining the big league club, Bochy said. ... San Francisco also got good news on RHP Ryan Vogelsong, who has missed the past two months with a broken right hand. Vogelsong played catch before Sunday's game and could resume throwing off a mound as early as the All-Star break. ... Home run king Barry Bonds was in attendance. ... RHP Zack Greinke (6-2) pitches the series opener in Arizona for the Dodgers. ... San Francisco RHP Tim Lincecum (4-9) will go against the Mets seeking his first win since June 4.