ST. PETERSBURG, Fla. — The Tampa Bay Rays appeared headed toward a fifth straight loss at home until things changed in the late innings.

Jake Elmore homered, Brandon Guyer had a two-run, pinch-hit single in a four-run sixth and the Rays rallied from a four-run deficit to beat the Boston Red Sox 7-5 on Wednesday night.

“We needed that one,” Tampa Bay Manager Kevin Cash said. “For the guys to come back from that deficit against that team, it just keeps impressing all of us, showing what they’re about.”

After a tiebreaking run scored on Desmond Jennings’ double-play grounder, Elmore gave the Rays a 7-5 advantage with his solo shot off Edward Mujica (1-1) on his first at-bat with Tampa Bay after having his contract purchased from Triple-A Durham before the game.

“That was about as good as you could draw it up,” Elmore said.

Tampa Bay had tied it at 5 in the sixth. Allan Dykstra drove in a run with the fourth consecutive single to start the inning before Logan Forsythe drew a four-pitch, bases-loaded walk that ended Joe Kelly’s game. Guyer hit his two-run single off Craig Breslow.

“I didn’t get ahead of hitters,” Kelly said, “In the sixth inning, I tried to get too fine and fell behind hitters, and had to throw it down the middle from there.”

Brad Boxberger (1-1) struck out all three batters he faced in the seventh to help Tampa Bay stop a four-game skid to begin a nine-game homestand. Steve Geltz pitched the ninth for his first career save.

Dustin Pedroia and David Ortiz homered for Boston.

Pedroia hit a two-run shot off Nathan Karns during a three-run third as the Red Sox took a 3-1 lead. Hanley Ramirez scored the other run from first base on Mike Napoli’s single that went off Karns’ glove and ended up in center field.

Ortiz made it 4-1 on a fifth-inning solo homer. The designated hitter moved past Joe DiMaggio into 46th place on the career RBI list with 1,538, and also broke a tie with Chipper Jones for 32nd place on the homer list with his 469th shot.

Mookie Betts had a run-scoring single in the sixth for Boston.

Steven Souza Jr. put the Rays up 1-0 with a first-inning homer. He departed after striking out to the end the sixth due to a right forearm cramp but is hopeful of playing Thursday.

Kelly gave up five runs and eight hits in 5-plus innings. He struck out seven and walked one. For the Rays, Karns allowed five runs, seven hits, four walks and had three strikeouts over six innings.

NOTES: RF Shane Victorino left after four innings with right hamstring tightness. … Ortiz has the second-most homers against Tampa Bay with 45. Only Alex Rodriguez, with 49, has more. Ortiz has the most RBI, 153, against the Rays. … He has 30 homers and 76 RBI at Tropicana Field, the most for any visiting player. … LHP Drew Smyly (left shoulder) will rejoin the Tampa Bay rotation Friday. … RHP Alex Cobb (forearm) could have his first bullpen session Friday. … 1B James Loney (oblique) is nearing a short minor league rehab assignment. … RHP Jake Odorizzi (2-1, 1.74 ERA) plans to attack the strike zone in Thursday night’s start against Boston, which entered Wednesday leading the AL with 63 walks. “They work at-bats,” Odorizzi said.