The Pirates rallied for four runs in the top of the ninth to tie the score 7-7.
The Nationals, who last won July 14 in the final game before the All-Star break, then got a one-out single by Kurt Suzuki in the ninth. After a force out, Harper connected for his 14th home run of the season off Bryan Morris (4-4), the fourth Pittsburgh pitcher of the game.
Ian Krol (1-0) allowed the final Pirates run-scoring hit but also got the final two outs in the ninth for his first major-league win.
Washington, benefiting from three Pittsburgh errors, scored four times in the first inning and three more times in the eighth with Adam LaRoche's two-run triple making the score 7-3.
The Nationals turned to closer Rafael Soriano to protect the four-run lead but he walked two batters and Jordy Mercer doubled in a run and Russell Martin singled in another. Kohl came in and walked Pedro Alvarez before getting an out. Josh Harrison followed with a two-run score, tying the score.
Harrison also had a two-run home run in the sixth inning.