BOSTON — The Red Sox held onto Mike Napoli at the trading deadline Friday, and he responded a few hours later with a two-run homer off the top of the Green Monster that led Boston over the Tampa Bay Rays, 7-5.

The Red Sox had already blown two leads when David Ortiz drew a two-out walk in the seventh inning and Napoli turned a one-run deficit into a 6-5 lead with a drive that landed on the ledge atop the 37-foot-high wall.

Napoli, a 33-year-old first baseman, had been on the trading block as someone who would be more useful to a contender than Boston, which is headed to a second straight last-place finish.

Another subject of trade talks, Alejandro de Aza, had two hits for Boston. Blake Swihart also had a pair of hits, doubling with one out in the eighth and scoring on a wild pitch to make it 7-5.

Junichi Tazawa (2-3) earned the win despite allowing John Jaso’s two-run double that gave Tampa Bay a 5-4 lead in the seventh. Koji Uehara pitched the ninth for his 23rd save.

Jake McGee (0-1) pitched one inning, giving up two runs on one hit and a walk.

The Red Sox scored three runs in the first thanks to a wild pitch and two errors. The Rays tied it in the third and, after giving the lead back in the fifth on a double by de Aza, went ahead in the seventh on Jaso’s pinch double.

The teams entered with 9-15 records in July, tied for the worst in the majors for the month. For the Rays, it was their worst July.

NOTES: Outfielder Hanley Ramirez (left shin) and third baseman Pablo Sandoval (left forearm) got the game off. Both will be evaluated again Saturday.