Nationals 4, Pirates 3: Wilson Ramos' walk-off double drove in Bryce Harper as Washington scored all of its runs after the seventh inning to stun Pittsburgh.

Washington battled back from a three-run deficit in the eighth and wasted little time securing its fifth straight victory in the ninth. Harper drew a leadoff walk against Justin Wilson (3-3) and moved ahead a base on a wild pitch moments later before Ramos ended it by sending a 1-2 fastball over the head of Gregory Polanco in right and into the Nationals' bullpen for a ground-rule double.

Kevin Frandsen tallied three hits, Adam LaRoche hit a two-run homer and Matt Thornton (1-3) struck out a pair during a flawless ninth to get his first win with Washington, which stretched its lead in the National League East to 6½ games. Gaby Sanchez finished with three hits and two RBIs while Starling Marte added a run-scoring double among his two hits as Pittsburgh dropped its fourth straight and fell one-half game behind San Francisco for the second wild-card spot.

Washington starter Gio Gonzalez struck out four consecutive batters before Pittsburgh broke through in the third when Polanco reached on a fielder's choice ground and Russell Martin walked ahead of Marte, who lifted a two-out flare just over a leaping Anthony Rendon at third to plate Polanco. Sanchez sent the next pitch between the hole at short and third to drive in two and complete the three-run frame.

Pittsburgh starter Jeff Locke recorded back-to-back strikeouts to strand a pair of runners in the fifth and Jared Hughes helped keep his line clean when he retired Ian Desmond for the final out of the sixth. Hughes got a double play to get out of the seventh, but Tony Watson lost the shutout and cost Locke a shot at his third win in as many starts in the eighth when Frandsen singled home a run and former Pirate LaRoche slugged his 18th homer.

GAME NOTEBOOK: The Nationals have backed Gonzalez with a total of two runs of support over his last five starts. … Locke yielded four singles and fanned six over 5 2/3 scoreless innings while Gonzalez, who is 0-5 with a 5.21 ERA over his last seven outings, allowed three runs on seven hits in five frames. … Washington won for the 14th time this season in its final at-bat.