Cody Ransom made his first homer of the season a memorable one, lifting a two-run shot off All-Star Clayton Kershaw in the seventh inning to help the Arizona Diamondbacks rally for a 4-3 win over the Los Angeles Dodgers and avoid a three-game sweep on Sunday.

Kelly Johnson hit two-run homer to the pool deck in right-center off Kershaw (13-5) in the first inning and Ransom's shot to left to put the Diamondbacks back ahead.

Ian Kennedy (14-3) pitched seven effective innings to win his sixth straight start and J.J Putz worked a perfect ninth for his 26th save to keep Arizona within a half-game of the NL West-leading Giants.

James Loney homered for the Dodgers and Kershaw had his five-game winning streak end after failing to reach the seventh inning.

Unlike the series' first two games, which included some shaky pitching performances, the finale figured to be tightly matched with two of the NL's best pitchers on the mound.

Kershaw, the NL strikeouts leader, had a superb first half of the season to get an invite to the All-Star game in Phoenix and had been dominant since the Midsummer Classic, allowing four earned runs in 30 innings to win all four of his starts.

Kennedy has been Arizona's best pitcher, ranking second in the NL in wins while setting a career high and has pitched at least seven innings 13 times. The right-hander beat the Giants on the road his last start, allowing two runs on six hits with seven strikeouts in eight innings.

It didn't start off like a matchup of No. 1 pitchers.

Kennedy needed a strikeout to escape a jam in the first inning and Kershaw opened the bottom half with seven straight balls before giving up the two-run homer to Johnson.

The two aces looked more the part after that, with Kershaw piling up the Ks — seven over four innings — and Kennedy getting the Dodgers to pound balls into the ground, setting up two nice plays in the hole by Ransom at shortstop.

Kennedy made a mistake in the fifth inning, leaving a 1-2 breaking ball up that Loney lifted over the pool for a leadoff homer. The right-hander got into more trouble the next inning, giving up an RBI double to Andre Ethier, then had to duck on a run-scoring single by Juan Rivera that put the Dodgers up 3-2.

Kennedy allowed three runs on six hits in seven innings, but left with the lead thanks to a mistake by Kershaw.

It came in the seventh inning, when the left-hander threw a belt-high fastball down the middle and Ransom hit it out to left for a two-run homer that put the Diamondbacks up 4-3. Kershaw knew the ball was out as soon as it left the bat, squatting on the mound and looking at the dirt before he even got to first.

Kershaw was pulled following a one-out double by pinch hitter Sean Burroughs after allowing four runs on five hits.

NOTES: Dodgers SS Dee Gordon was out of the lineup after leaving Saturday's game with a jammed right shoulder. He was injured while taking a tumble running down Kelly Johnson in the third inning. ... Diamondbacks manager Kirk Gibson was back in the dugout after leaving in the second inning of Saturday's game because he was feeling sick. ... The Dodgers move on to face Philadelphia at home in a three-game series that starts Monday. Right-hander Hiroki Kuroda gets the nod for Los Angeles after beating San Diego his last start for his first win since July 1. Kuroda pitched seven scoreless innings on Tuesday, allowing four hits and striking out eight. ... Arizona continues its homestand with four games against the Astros, starting Monday. Right-hander Daniel Hudson, coming off a big road win over the Giants his last time out, gets the ball for the Diamondbacks against Wandy Rodriguez. ... Hudson is 11-3 with a 3.25 ERA over his last 19 starts.