Lannan launched a two-run shot in the second for his third hit of the season and the 19th of his 5-year career. One of those hits was a two-run single in his previous start July 16.
He went 6 1/3 innings to get the victory, giving up a run on three hits with six strikeouts. Lannan, who has never won more than nine games in a single season, improved to 7-6.
The Nationals were clinging to a 3-2 lead heading into the ninth but broke things open when they loaded the bases with one out and Hairston hit his fourth home run of the year.
Lannan's homer came off Hiroki Kuroda (6-12), who gave up three runs on seven hits in 6 1/3 innings to suffer the loss. He struck out seven.
The Nationals had lost seven of their previous nine games.