The Giants trailed 4-0 after 2 1/2 innings but tied the score with a 3-run eighth inning. Washington had runners on in each extra inning except the 13th, which was pitched by Javier Lopez (3-1), who also got the final out in the 12th.
Stewart walked to open the 13th against Craig Stammen (2-1) and after an out, Andres Torres singled. Miguel Tejada reached on a force out at second with Stewart taking third.
Sanchez then sliced a 1-1 pitch to right for the game-winning single.
San Francisco had just two hits until Aaron Rowand led off the seventh with his second home run of the year. The Giants added three runs in the eighth with Aubrey Huff knocking in two runs and scoring the third on a Nate Schierholtz base hit.
Washington took its early lead off Giants starter Tim Lincecum, who allowed four runs, five hits and three walks in five innings. Michael Morse opened the Nationals' second with his ninth home run of the season and knocked in two runs in a 3-run third inning.