The D-backs have now dropped eight of their last 11 games, while the Giants managed to halt their losing streak at six.
With one out in the ninth the D-backs rallied against Giants closer Sergio Romo. Pinch-hitter Roger Kieschnick doubled to right-center and Didi Gregorius beat out an infield chopper to put runners at first and second.
A groundout by Gerardo Parra advanced the runners and left first base open as Goldschmidt walked to the plate.
San Francisco manager Bruce Bochy visited the mound to talk to Romo.
"Sergio wants to go after everybody," Bochy said. "That's his mentality."
So rather than intentionally walk Goldschmidt, who was 4-for-10 in his career against Romo, to load the bases for Aaron Hill, who is 0-for-6 against the right-hander, the Giants went after Goldschmidt.
"It's a tough call," Bochy said. "You're going against the book a little bit, but you got to deal with one of the best hitters in the game."
Goldschmidt said he was not surprised the Giants decided to pitch to him and he jumped on the first pitch -- a slider -- and lined it to right. Off the bat, it looked like the ball might split the gap in right-center, but right fielder Hunter Pence made a routine running catch to end the game.
"He's mostly fastball-slider to righties so you're just trying to look for something in the middle of the plate and it didn't quite break to the outside corner, so I took a swing at it," Goldschmidt said of the pitch. "Right off the bat, I thought it had a chance, but Pence had a good bead on it and ran over there and I think got to it pretty easily."
The Giants were able to jump on D-backs starter Brandon McCarthy, whose winless start streak is now at nine.
McCarthy (1-10) allowed three runs in the first as the Giants sent nine men to the plate in the inning and forced the right-hander to throw 32 pitches.
McCarthy allowed a run in the second and five overall in his five innings of work.
"They hit him hard the first two innings," D-backs manager Kirk Gibson said. "He settled down a bit, but overall just too much."
McCarthy's lone win of the year came May 3 and he is 0-5 since that time. Overall his ERA is 5.38.
"The frustrating thing is I don't have any more answers this week than I did last week or the week before," McCarthy said. "I don't know what the heck I'm doing wrong. I really truly don't. Other than the results are poor, I don't know what in my routine, I don't know what I'm doing in games is wrong. It just doesn't seem to be coming together.
"You want to beat your head against the wall."
While the D-backs can easily look back at opportunities missed during the game -- they were 4-for-11 with runners in scoring position and left eight on base -- the fact is the Giants, too, could have had more runs as they stranded 10.
"We had some good opportunities," Gibson said. "We had some good at-bats even down to the last at-bat with Goldy. It was a great approach. We fought our way back and got the winning run to the plate and he smashed it. It just wasn't our night."