The Diamondbacks appeared to be on their way to a victory in the ninth when Gerardo Parra's RBI single put them in front 2-1. But the Giants' Brandon Crawford clubbed a solo homer in the bottom of the frame off David Hernandez (1-1), sending the contest into extra innings.
Goldschmidt drew a one-out walk in the 10th from Chad Gaudin (0-1) and took third on Cody Ross' double into the right field corner. After Gaudin intentionally walked Miguel Montero to load the bases, Javier Lopez came in to pitch, and pinch-hitter Nieves lofted a fly to center, sending home the decisive run.
Matt Reynolds yielded a two-out double to Brandon Belt in the bottom of the 10th but then retired Joaquin Arias to end the contest, picking up his second save as the Diamondbacks took the rubber match of a three-game series.
Crawford finished 3-for-4 with two RBI for San Francisco, which concluded a 4-2 home stand with a second straight loss.