Cain (3-4) allowed a run on eight hits and struck out six for the Giants, who have won their last seven road games. Brian Wilson got the final four outs for his 18th save to tie Chicago's Kerry Wood for most saves in the National League.
Lastings Milledge scored two runs for the Nationals, losers in eight of their last nine games. Jesus Flores drove in a run.
Tyler Clippard (0-1) was called up from Triple-A Columbus before the game and got the start and the loss in his Washington debut, after allowing all three runs and all five hits in 4 1/3 innings.