The Red Sox had forged a 3-2 lead in the seventh on a three-run homer by Adrian Gonzalez, but the Rays came back in their last at-bat.
The rally made a winner of Jake McGee (2-1), who had induced Gonzalez to ground out to end an eighth-inning threat.
Fernando Rodney earned his 16th save for the Rays, who took the rubber match of a three-game series and grabbed a share of first place in the American League East with Baltimore.
Red Sox starter Clay Buchholz yielded two runs on eight hits over seven innings.
Notable deaths of 2014 [PHOTOS]