Pablo Sandoval moved into the cleanup spot and broke out of his slump.

Sandoval and Hector Sanchez hit RBI singles in the 10th inning, and the San Francisco Giants recovered to beat the Los Angeles Dodgers 7-4 on Sunday after closer Sergio Romo gave up a tying homer to Hanley Ramirez.

"I'm happy, but I'm not satisfied. I just want to do better for my teammates and the team," Sandoval said. "You've got to keep focused and keep working hard, because everything is going to come back. So I don't worry too much about it. I just try to stay positive and keep fighting and have the faith that I can do it."

Sandoval, who began the day hitting .173, was 3 for 5 with two RBI. His other six RBI this season came when he was batting third.

"He was pretty excited about where he was hitting today, and he asked me how much I had to drink last night," Giants manager Bruce Bochy said. "I know it's been frustrating for him, but he's been making better contact, and it just seems like he's got some confidence going. More than anything, he's got a little bit of a swagger right now, and he's got to keep that. Hopefully, this will be something he can build on."

San Francisco needs Sandoval to start producing in the middle of the lineup, especially with Brandon Belt sidelined for at least six weeks because of a broken thumb.

"It was nice to see him go out there and get three knocks," Giants pitcher Tim Hudson said about Sandoval. "You know the great player is in there. And for him to get back to being that great player, it's between the ears. Once he gets a little confidence going and believing in himself, he'll be OK. We believe in him, and it's something we've been saying for a long time."

Sandoval's run-scoring double in the first against Clayton Kershaw ended an RBI drought of 20 games and 68 at-bats for the switch-hitting third baseman since April 16 against the Dodgers. It was only the third hit with a runner in scoring position in a span of 27 at-bats for Sandoval, who clapped his hands as he went into second base standing up.

"When you get the RBI to get ahead in the first inning against a tough pitcher like that, you have to be happy," Sandoval said. "I'm happy that we're winning and that we're in first place."

Brandon Hicks hit a two-run shot against the two-time NL Cy Young Award winner, and Buster Posey had an RBI single for the NL West leaders, who beat the defending division champions for the seventh time in 10 meetings this season.

The Giants finished their road trip 7-3 and played errorless ball over the last five games.

Romo (3-0) came on in the ninth to try and protect a 4-2 lead for Hudson. The right-hander struck out Yasiel Puig after a one-out double by Dee Gordon, but Ramirez lined his fifth homer to left field on an 0-1 pitch. The blown save was Romo's first this season in 13 chances.

Angel Pagan drew a leadoff walk in the 10th from Dodgers closer Kenley Jansen (0-2). Hunter Pence singled and both runners advanced on a wild pitch before Posey was intentionally walked. Sandoval and Sanchez delivered clutch hits, then Posey scored on J.P. Howell's wild pitch.

Jean Machi got three outs for his first major league save.

Hudson allowed two runs and eight hits over six innings, including a leadoff homer in the sixth by Puig that extended his career-best hitting streak to 11 games. The right-hander went fewer than seven innings for the first time in eight starts this season.

Kershaw allowed three runs and seven hits in his third start of 2014 and first at Dodger Stadium. The three-time defending major league ERA champ struck out nine and walked none in his second outing since coming off the disabled list. He also had nine Ks and no walks last Tuesday at Washington, when he pitched seven scoreless innings in an 8-3 win.

Sandoval led off the seventh with a single and Hicks followed with his seventh homer, a drive to left field on an 0-2 curveball. The Giants have 45 homers through 38 games, their highest total at this stage of a season since 2001 (51).

Hicks also went deep Thursday against Josh Beckett in the Giants' series-opening win.

"It's frustrating," Kershaw said. "The team played so well and we battled all day. Guys were making plays, getting big, clutch hits, moving guys over and playing the game the right way. So for me to give up that home run after just getting the lead right back is disappointing. I've got to shut the door right there."

Los Angeles grabbed a 2-1 lead in the sixth. Puig drove Hudson's first pitch of the inning into the Dodgers' bullpen in left to tie it 1-all and flipped his bat after making contact.

"It is what it is. I mean, boys will be boys," Hudson said. "He's an emotional player and he hit the (stuffing) out of it. I probably would have flipped the bat, too."

Ramirez then legged out a double and Adrian Gonzalez followed with an RBI single that ended a 2-for-33 slide. Gonzalez tried to score from second on Drew Butera's single to center, but Pagan threw him out at the plate.

NOTES: Beginning with Monday night's game against the Marlins, fans entering Dodger Stadium will be going through metal detectors at selected gates for a limited number of games the rest of the season. The new security measures, which Major League Baseball is implementing in conjunction with the Department of Homeland Security, are expected to be in all big league ballparks by next year. . . . The Giants don't return to Dodger Stadium until Sept. 22.