Justin Upton never had hit a game-ending home run in the majors — until the first pitch he saw from Santiago Casilla in the 10th inning Thursday night.

The slugger's opposite-field shot gave Arizona a 3-2 victory over San Francisco, a much-needed win that snapped the Giants' five-game winning streak against the Diamondbacks. The first four of those games had been by one run.

This time, Arizona got the one-run triumph.

"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."

Casilla said Upton hit a good pitch.

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

Upton had a pair of soft singles to bring home the winning run this season. This hit was a bit more decisive.

"We didn't play good, but we won the game,'" said Diamondbacks manager Kirk Gibson, who led the celebration at home plate to greet Upton. "We didn't play good at all and we need to play a lot better. You get frustrated with things like that, but we hung in there and J-Up picked us all up. J-Up and Ian (Kennedy), and CY (Chris Young) had a great game again."

Kennedy gave up only an unearned run on four hits and struck out 10 with one walk over eight innings to stake the Diamondbacks to a 2-1 lead. But Pat Burrell's RBI sacrifice fly in the ninth tied it as J.J. Putz, struggling lately after an outstanding start to the season, blew his third save in 21 opportunities.

The Diamondbacks snapped San Francisco's five-game winning streak against them.

Young homered for Arizona for the second night in a row and David Hernandez (3-2) pitched a scoreless 10th to get the victory.

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," manager Bruce Bochy said. "We were real close to getting this last one. Guys battled hard. 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 winning 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.

Kennedy, on the brink of going 8-2 for the season, acknowledged how tough it is to sit in the dugout and watch his victory evaporate.

"It's hard to watch," he said, "because you really can't do anything about it because you're sitting on the side. You get to look back on it as I did my job the best I possibly could. As long as we win, that's the main part."

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.

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.