Gio Gonzalez threw seven shutout innings to end a seven-week winless drought.
The Nationals won their third in a row, getting a two-run homer from Harper in the fourth, a three-run blast from Werth during a four-run sixth and a three-run shot from Desmond in the seventh.
Gonzalez (8-6) held the Marlins to three hits and three walks with eight strikeouts. Tanner Roark worked the final two innings to complete the shutout.
The first two home runs were given up by Tom Koehler (3-9), who allowed six runs on seven hits with four walks in five-plus frames.
The Marlins, who have won just 49 games this season, became the first team in the National League to be officially eliminated from their division pennant race.