Evan Longoria's RBI single in the fifth inning 3, in the third test of a crucial four-game set.
Ben Zobrist hit a two-run homer for the Rays, who trail Boston by three games in the wild card.
Jeff Niemann (11-7) gave up two runs on four hits over five innings to pick up the win. He struck out six and walked two.
Jon Lester (15-8) allowed four runs on five hits over seven innings to take the loss. He struck out five and walked four.
"It was a lot of deep counts. He's so good and he competes so much, he finds a way to get you that deep in the game. But he's not hitting his spots as consistently as he usually does," said Boston manager Terry Francona of Lester.
The Rays jumped out to a 2-0 lead in the first inning. Desmond Jennings walked, moved to second on a sacrifice bunt, advanced to third on Longoria's groundout and scored on Zobrist's blast to left.
Tampa Bay tacked on a run in the third to make it a 3-0 game. Jennings hit a one-out double to left, moved to third on B.J. Upton's groundout and crossed the plate on a wild pitch.
Boston got two runs back in the home third. Carl Crawford led off with a single and crossed the plate on Mike Aviles' double to left. Aviles advanced to third on a sacrifice bunt and scored on Ellsbury's sacrifice fly.
The Rays scored a run in the fifth to make it a 4-2 contest. Brandon Guyer and Upton singled to put men on first and second with two outs. Longoria followed with an RBI single to right.
The Red Sox put runners on the corners with two outs in the sixth, but Jarrod Saltalamacchia popped out.
Boston pushed across a run in the seventh to make it a 4-3 game. With men on second and third and one out, Ellsbury knocked in a run when he grounded out. Dustin Pedroia struck out swinging to end the inning.
Joel Peralta worked around a two-out single in the ninth to pick up his fourth save of the season.
Tampa Bay has won 11 of its 17 matchups with the Red Sox this season and is 6-2 at Fenway, where it won six of nine a year ago...Niemann beat the Red Sox with a complete-game effort back on August 16 and is 4-2 in seven starts against them...Lester fell to 10-6 lifetime against the Rays...Boston went 0- for-8 with runners in scoring position and stranded six.