LOS ANGELES -- Until the very end, no lead was safe for the Los Angeles Dodgers.

The Dodgers repelled two threats in the final two innings to defeat the Arizona Diamondbacks 4-3 on Friday night in front of 42,831 at Dodger Stadium.

Left fielder Matt Kemp and shortstop Hanley Ramirez each had two hits, with Kemp hitting his first home run since May 10, and left-hander Clayton Kershaw won his third consecutive start.

Kershaw (6-2) struck out seven batters and induced 10 groundouts in seven innings while allowing one run, eight hits and one walk.

"It was a struggle," Kershaw said. "They made me pay for every mistake."

Arizona second baseman Aaron Hill and right fielder Gerardo Parra each went 3-for-5, with Hill driving in two runs. But the Diamondbacks lost their third successive game and their fourth in their last five.

Right-hander Chase Anderson (5-1) was trying to become the first rookie since Jered Weaver in 2006 to win his first six decisions. He allowed two runs, seven hits, four walks and a hit batter in five innings while striking out four.

"I hate to lose," said Anderson, who lacked command of his fastball for the first time this season, even when he was pitching with Double-A Mobile.

"Usually, I'm able to establish it by the fourth inning. But I could just never find it."

The Dodgers took a 3-1 lead in the bottom of the seventh when first baseman Adrian Gonzalez singled Ramirez home. But the Diamondbacks narrowed their deficit to one run in the top of the eighth and had a chance to tie the score against right-handed reliever Brian Wilson.

Catcher Tuffy Gosewisch's single scored Hill and sent third baseman Martin Prado to second base. But left-hander J.P. Howell relieved Wilson and struck out center fielder David Peralta before pinch-hitter Miguel Montero hit a long fly ball that center fielder Andre Ethier caught at the warning track.

Again, Los Angeles built a two-run lead in the bottom of the eighth when catcher A.J. Ellis scored on pinch-hitter Justin Turner's line-drive single to right field. Again, Arizona closed within one run and threatened to get more against right-handed closer Kenley Jansen.

Hill's single sent Parra home and put first baseman Paul Goldschmidt at second with one out. But Jansen struck out Prado and got left fielder Cody Ross on a flyout for his 18th save.

The Dodgers took a 2-0 lead in the bottom of the second inning when Kemp hit his sixth home run of the season, a two-run drive over the fence in left-center field off Anderson.

"We've struggled to score early," Dodgers manager Don Mattingly said. "We've been behind a lot early at home, so it was good for us to get that lead."

In the top of the third, the Diamondbacks scored a run and put the potential tying run in scoring position. Shortstop Chris Owings hit a leadoff double and came home two outs later on Hill's double. Hill moved to third base on a wild pitch but stayed there when Prado struck out.

Owings made an unassisted double play in the bottom of the third. With one out and Ramirez going from first base on a 1-1 pitch, Owings caught a line drive from Gonzalez, then tagged Ramirez to end the inning.

Spontaneous cheers and applause erupted in the bottom of the sixth when fans learned that the Los Angeles Kings won the Stanley Cup. Between the sixth and seventh innings, the video screen in left field showed the Kings scoring the winning goal in overtime and celebrating.

NOTES: Arizona 1B Paul Goldschmidt began Friday leading the National League with 83 hits, 27 doubles and 149 total bases. He also ranked second with 48 RBIs and 49 runs scored. ... The Diamondbacks recalled OF Tony Campana from Triple-A Reno after placing OF Ender Inciarte on the seven-day concussion list Friday. ... Arizona 3B Martin Prado needs four hits for 1,000 for his career and five RBIs from 400. ... The Dodgers activated C A.J. Ellis from the disabled list Friday and sent C Tim Federowicz to Triple-A Albuquerque. Ellis made Friday night's lineup. ... SS Hanley Ramirez returned to the starting lineup Friday night. An inflamed joint in his right shoulder limited Ramirez to a pinch-hitting appearance Wednesday night in Cincinnati and kept him out of Thursday's game. ... RHP Chad Billingsley has a partially torn flexor tendon in his right elbow, the Dodgers announced Friday. Surgery is one option being considered. ... The Dodgers signed 12 of the players they selected in last week's draft on Friday. Third-rounder RHP John Richy from UNLV was the highest pick to sign Friday.