Washington won the final two games of the three-game interleague series after losing four in a row.
The Nationals, who have not had a winning season since moving to Washington from Montreal in 2005, became the second National League team this year to reach 40 victories.
Washington leads the NL East by 3 1/2 games over the New York Mets.
Espinosa, who has missed only one game all season, came into the contest batting just .227. He had two hits Thursday and reached on a walk in the third inning before scoring on Michael Morse's sacrifice fly to tie it at 1-1.
The sixth-inning double by Espinosa gave Washington a 4-2 lead and Roger Bernadina added a run-scoring double in the seventh for the Nationals.
Washington used six pitchers to fight off the Rays with the win going to starter Gio Gonzalez (9-3). He gave up two runs on seven hits in six innings and picked up the victory thanks to Espinosa's double in the sixth.
Tyler Clippard worked the ninth for his 11th save.
Reliever Joel Peralta (0-3), facing an eight-game suspension for using a foreign substance on his glove, gave up the two runs in the sixth and suffered the loss.