In an unusual game which saw Rays boss Joe Maddon hook starter Jeremy Hellickson in just the second inning, Tampa Bay snuffed out a number of early Boston opportunities before taking the lead in the sixth when David DeJesus scored Yunel Escobar.
But the Sox fought right back and tied the game up when Xander Bogaerts scored on a wild pitch from Joel Peralta, then took the lead as Shane Victorino legged out an infield single to plate Ellsbury.
Dustin Pedroia scored Bogaerts with a sacrifice fly in the ninth and closer Koji Uehara made amends for surrendering a walk-off home run in game three by recording a four-out save as the Sox took the best-of-five divisional series 3-1.
They will have to wait to find out their ALCS opponents after the Detroit Tigers battled back to force a decisive game five in their series with Oakland by beating the Athletics 8-6.
Jhonny Peralta and Victor Martinez homered for the Tigers as they got to the Oakland bullpen to the tune of five runs, leaving the series poised at 2-2.