PHOENIX -- Paul Goldschmidt turned a tough night into a triumph, and not for the first time in what so far has been a magnificent season.
The Giants were poised to give Matt Cain a victory and send Patrick Corbin to his first loss in 10 decisions before Goldschmidt lined a 2-0 pitch from Jeremy Affeldt over the right-field fence for his 15th home run of the season. Goldschmidt, hitting .332, boosted his NL-leading RBI total to 57.
He had hit into two double plays and struck out in his previous at-bats Friday.
"Think about it -- two double plays, a strikeout and he hits a three-run home run," Arizona manager Kirk Gibson said. "Last week we were in Chicago -- three strikeouts against (Jeff) Samardzija and a grand slam to help us win a game. He's pretty special. He really is."
The quiet Goldschmidt got a curtain call from the crowd, waving from the dugout.
"That's cool," he said. "It's fun any time you get the win, but obviously a come-from-behind win. A great crowd tonight, so it was a lot of fun."
Arizona stayed 2 1/2 games ahead of Colorado in the NL West. San Francisco dropped to 3 1/2 back.
Cain went seven innings plus a batter, allowing a run on four hits with three walks. Corbin gave up a run on seven hits in 7 1/3 innings, striking out four with no walks.
Cain left after A.J. Pollock beat out an infield grounder to third for a base hit. With Affeldt on in relief, Pollock advanced to second on pinch hitter Wil Nieves' groundout to second. Affeldt got Gerardo Parra looking, then Willie Bloomquist walked to bring up Goldschmidt. Coming off a road trip in which he hit two grand slams, he caught up with a fastball over the plate. Goldschmidt had been hitless in five previous at-bats against Affeldt.
"It was a dumb pitch -- dumb location, dumb selection, stupid," Affeldt said. "I didn't want to throw it there. For a strike pitch, it wasn't that bad, but that is not what I am trying to do to him. He is too hot of hitter, and when you make dumb pitches and you throw to a dumb location, they are going to make you look bad. I take full responsibility. The loss is on me. Cain threw really well. He didn't deserve that. It is completely my fault."
Giants manager Bruce Bochy stuck with Affeldt, saying he "has been throwing the ball as well as anybody."
Besides, Goldschmidt has been hitting right-handed hitters better than lefties.
"That guy is hitting lefty and righties if you are looking at the numbers," Bochy said. "He (Affeldt) just made a mistake there. It all started with the ground ball they beat out and of course the walks set up that. You are asking for trouble."
Cain settled down after a 31-pitch first inning, in which he walked three and gave up a single but held Arizona scoreless.
Goldschmidt grounded into a double play, leaving a runner at third with two outs. Cain walked the next two batters to load the bases but got Martin Prado on a broken-bat groundout to end the inning.
Didi Gregorius led off the Diamondbacks' fifth with a double and, with a head-first slide, barely beat Gregor Blanco's throw on A.J. Pollock's fly out to center to put a runner on third with one out. But Gregorius tried to score on Corbin's weak grounder to first and was easily thrown out by Brandon Belt.
Corbin retired 12 in a row before Pablo Sandoval's broken-bat single with one out in the seventh. Belt singled sharply to right to put runners at first and second. Sandoval took third when pinch hitter Joaquin Arias flew out to right, then Blanco's first-pitch single to center put San Francisco up 1-0.
Goldschmidt said Corbin has "been unbelievable for us this year."
"When he came out of the game, we felt awful," Goldschmidt said. "We were trying to score for him. We had a few opportunities early and weren't able to come through. But he did a great job. That's what it takes when you have those come-from-behind wins. You need to keep putting up zeroes and give the offense a chance."
One more chance was all Goldschmidt needed.
First-base umpire Jerry Layne, the crew chief, aggravated a rib injury and left the field near the start of Arizona's half of the third inning. The crew went with three umpires the rest of the game. ... Corbin was trying to be just the third pitcher in 25 years to start 10-0. Roger Clemens (11 in 1997) and Aaron Small (10 in 2005) were the others. ... San Francisco has won eight of its past nine at Chase Field. ... The temperature was 108 degrees outside, 83 inside Chase Field at first pitch. ... Madison Bumgarner (4-4, 3.46 ERA) starts for the Giants and Trevor Cahill (3-6, 3.27) goes for Arizona on Saturday night. ... San Francisco has lost seven of 10.