Both home runs came off Vance Worley (11-3), who had lost just once in his first 16 starts this season. The Nationals handed Worley his second straight defeat.
Ramos hit his 14th homer of the year in the first inning and Espinosa's blast in the sixth put the Nationals ahead for good at 4-3. A three-run Washington eighth that featured an RBI single by Ramos allowed the Nationals to survive a two-run homer by John Mayberry in the bottom half of the inning.
Washington picked up its fourth straight victory and the win went to John Lannan (10-13), who gave up three runs on eight hits in five innings. It was only his second win in 17th career starts against the Phillies.
National League East champion Philadelphia has lost five in a row for the first time this season and still needs two wins to get to 100.