The San Francisco Giants came ever so close to another win in their mastery of the Arizona Diamondbacks.

One swing by Justin Upton ruined what could have been a comeback victory that completed a three-game sweep for the Giants in the desert.

Upton's first career game-ending home run — on the first pitch he saw from Santiago Casilla in the 10th inning — gave the Diamondbacks a 3-2 win Thursday night, their first victory over San Francisco in six tries.

The Diamondbacks pulled within 1½ games of first-place San Francisco in the NL West. Still, the Giants won two of the three after Arizona narrowed San Francisco's lead to a half-game.

"It is good to come in and get the series," Giants manager Bruce Bochy said. "We were real close to getting this last one."

Arizona's Ian Kennedy held San Francisco at bay for eight innings, allowing only an unearned run on four hits while striking out 10 and walking one. The Diamondbacks took a 2-1 lead into the ninth, but closer J.J. Putz couldn't hold it, blowing his third save in 21 tries.

Pat Burrell's RBI sacrifice fly brought in the tying run and sent the game to extra innings.

David Hernandez (3-2) shut down the Giants in the top of the 10th, then Casilla (0-1) got Stephen Drew to ground out to start the Arizona half of the inning.

That brought up Upton, who got the pitch he was looking for and sent it over the opposite-field fence in right. Casilla said it was a good pitch.

"Outside, down," he said. "He is a pretty good hitter. That is where I was trying to throw it — outside, down. He got it."

The first four of those five straight San Francisco wins over Arizona were by one run. This time it was the Diamondbacks' turn to win a close one.

"It's awesome," Upton said, explaining the feeling of hitting one over the fence and rounding the bases in that situation, "the fact that we'd just dropped two games and to be able to win in that fashion. It's hopefully a little bit of a boost for the team."

Chris Young homered for Arizona for the second night in a row.

"Guys battled hard," Bochy said. "Kennedy has been throwing the ball well. We couldn't do much off him. We score one run on an error. The closer, he is tough, and we found a way to get a run off him. Then Burrell smoked the ball and they almost misplayed that. Offensively we just had too many guys who took oh-fers today. That makes it hard to get runs on t he board."

Upton also had a single and double. He had two game-ending singles in his career, but never a home run until Thursday night.

Putz, charged with two runs in Arizona's 5-2 loss on Wednesday night, has been scored upon in four of his last seven appearances after setting a franchise record by starting the season with 16 straight saves.

San Francisco started the ninth with Pablo Sandoval's single past second baseman Kelly Johnson, then Aubrey Huff bounced one over the head of first baseman Juan Miranda to put runners at first and third with no outs. Putz struck out Cody Ross but Burrell lofted one to left to bring in the tying run before Brandon Crawford flew out to left to end the inning.

Upton, who also doubled in the eighth, led off the fourth with a single, then on a 1-2 pitch, Young pulled one down the left-field line into the Arizona bullpen, his 14th home run to give the Diamondbacks their first lead of the series. 2-0.

San Francisco responded with an unearned run in the fifth. Ross and Nate Schierholtz started the inning with singles. Brandon Crawford fouled out to the catcher, then in a 10-pitch at bat, Eli Whiteside struck out swinging. San Francisco sent both runners on the play, and catcher Miguel Montero threw wild to third, allowing Ross to score and Schierholtz to advance to third. Giants starter Ryan Vogelsong struck out to end the inning, with the Diamondbacks still on top, 2-1.

Vogelsong allowed two runs on six hits in six innings, striking out six and walking one.

"What a great job he did," Bochy said. "It is not easy pitching in a park like this, especially with the roof open, balls flying. Both teams pitched well today. Voggy gave us a great effort. it would be nice to give him a win but he certainly did all he could."

NOTES: Giants OF Darren Ford (sprained left ankle) began a rehab assignment Thursday night with Triple-A Fresno. ... Arizona entered the game leading the NL and second in the majors, behind Boston, in extra-base hits. ... The game was the ninth of 18 scheduled between the Giants and Diamondbacks this season.