Sandoval finished 3-for-5 with three RBI for the Giants, who captured three of four in this set. Hunter Pence added a pair of hits and scored twice and Joe Panik had three hits for San Francisco, which has won four of five overall.
Daniel Murphy hit a two-run homer and Juan Lagares had three hits for the Mets, who have lost four of six.
After trailing 2-0 and 3-2 earlier in the game, the Giants took the lead for the first time in the game during their last at-bat. With two outs and the game tied 3-3, Gregor Blanco began the rally with a two-out base hit. After New York reliever Jenrry Mejia walked Buster Posey, Sandoval's flare down the left-field line bounced out of play for a ground-rule double, plating Blanco with the go-ahead run.
Brandon Belt grounded out to end the inning and strand runners on second and third, but the damage was done. Santiago Casilla came on for the ninth and set the side down in order to earn his ninth save of the season.
Sergio Romo (5-3) got the win, tossing one inning of relief.
Mejia (5-4) suffered the loss, surrendering one run on two hits with a walk and a strikeout in one inning of work.
The Mets got going in the first when Murphy hit a two-run homer off San Francisco starter Tim Hudson, but the Giants answered in the third when Sandoval's two-run double off New York starter Dillon Gee tied the game.
Murphy scored the go-ahead run on a David Wright infield single in the fifth. However, the Giants answered in the seventh against reliever Jeurys Familia. Pence started the inning with a triple and later scored on a wild pitch by the Mets' right-hander.
New York escaped further damage in the frame when Lagares cut down Blanco at the plate trying to score on a Sandoval single for the first out and reliever Josh Edgin got out of the frame by inducing an inning-ending double play.
Hudson received a no-decision, allowing three runs -- two earned -- on seven hits in five innings. The right-hander struck out two and walked two. He was making his first appearance at Citi Field since suffering broken ankle at the venue last season.
Gee also avoided a decision. He was charged with two runs on five hits in 5 2/3 innings. He struck out six and walked three.