Two batters later, Ryan Zimmerman drove in two more runs with a double.
Valdez was replaced by right-hander Justin Miller, who gave up an RBI double to Elijah Dukes. Nieves came up for the second time and hit a sacrifice fly to score Adam Dunn.
"Effort hasn't been the issue with these guys. They have not given up at all, offensively. They have done this a few times earlier in the year. It was huge," manager Manny Acta said. "It was also big that Zimmerman got that big hit for us since he has been scuffling a little bit. We need a guy like him to step up in situations like that."
Right-hander Craig Stammen started for the Nationals and kept them in the game. He lasted six innings and gave up five runs -- four earned -- on seven hits. He got off to a slow start by giving up a first-inning home run to leadoff hitter Aaron Rowand. After giving up a one-out double to Randy Winn in the same inning, Stammen retired 11 straight batters.
"I like the fact that he threw strikes," Acta said of Stammen. "Even when he was hit a little bit or we didn't play the best defense behind him, he didn't throw that many pitches and pitched six innings for us."
While Stammen was settling down, the Nationals were figuring out Giants right-hander Tim Lincecum, last year's Cy Young Award winner. At the hitters meeting before the game, Nationals hitting coach Rick Eckstein told the position players to lay off Lincecum's fastball out of the strike zone and eliminate being behind in the count because of his breaking ball.
The plan worked for the most part because Lincecum wasn't his dominating self, lasting 6 1/3 innings and giving up four runs, two of which were earned.
"I think we put a good hitting plan against him. Our guys hung in there against a very tough pitcher," Acta said.
The Nationals ended up taking a 2-1 lead against Lincecum. In the second inning, Josh Bard took the first pitch and hit a home run to tie the score at 1.
An inning later, Adam Dunn scored the go-ahead run when Dukes' grounder went under the glove of third baseman Juan Uribe.
"It's frustrating. I gave up a lot of base hits where I don't think I should have," Lincecum said.
But San Francisco would take the lead in the top of the fifth inning. With runners on second and third, Stammen walked Rowand, but the ball went by Bard for a wild pitch. Fred Lewis scored easily. Bard threw the ball away trying to get Lewis at the plate and Uribe ended up scoring the go-ahead run.
Two batters later, Rowand scored on a single by Winn.
An inning later, Uribe doubled home Pablo Sandoval to make it a 5-2 game.
But the Nationals would make their comeback.
Washington made it a one-run game against Lincecum in the top of the sixth inning. Willie Harris scored on a forceout by Hernandez, who scored on a sacrifice fly by pinch-hitter Ronnie Belliard.
Washington would win the game two innings later. The victory came a few hours after the team dismissed pitching coach Randy St. Claire.
Asked if the dismissal was a wakeup call for the players, Acta said, "Well, it's only been a game. I don't think so. In the history of the game, they know the coaching staff can be wiped out and nothing is going to happen to them. The only moving parts are the guys who have not been performing and they can be moved. As individuals, I think they know they have to play better and have the pride to do it. Those guys are trying. They will get better."