BOSTON — Mired deep in last place in the AL East standings, the Tampa Bay Rays rallied late and kept the Boston Red Sox from moving within a game of the division lead.

Luke Maile’s two-run homer in the seventh put Tampa Bay into a 3-3 tie and set up Evan Longoria’s game-winning solo shot in the eighth as the Rays rallied to beat the Red Sox 4-3 on Tuesday night.

“Exciting win. That was a nice come from behind,” Tampa Bay manager Kevin Cash said.

The Rays have won all of 56 games this season and needed to come from behind for 27 of them. Tuesday’s rally did nothing to help the Rays’ standing within the division, but it did keep Boston from picking up a game on first-place Toronto after the Blue Jays’ 5-3 loss to Baltimore.

Boston remained two games behind Toronto for the AL lead and the Orioles are just one game behind the Red Sox.

“This is what happens down toward the end of the year,” Boston shortstop Xander Bogaerts said. “Some teams are battling to push you out of the playoffs and some teams are battling to stay in it.”

Bogaerts doubled during Boston’s two-run sixth inning that briefly put the Red Sox up 3-1. The Rays tied it in the seventh with and unlikely power source at a crucial moment. Maile delivered his second career homer with two outs in the seventh, driving a 1-2 pitch from Drew Pomeranz out to left.

It was the last pitch of the night from Pomeranz, who was one strike from cruising into the eighth with a two-run lead.

Longoria finished up the rally in the eighth with his 31st homer of the season, a shot that cleared the Green Monster altogether and stuck Boston reliever Clay Buchholz (5-10) with the loss.

“It is what he does. That’s why he’s such a special player,” Cash said. “We’re thankful we’ve got Longo on our side.”

Pomeranz and Tampa Bay starter Jake Odorizzi both pitched well but didn’t factor in the decision.

Enny Romero (2-0) pitched a scoreless seventh with two strikeouts and got the win. Alex Colome survived a hit and a passed ball in the ninth while picking up his 29th save.

Pomeranz struck out eight in 6 2/3 innings, allowing three runs, five hits and two walks. Odorizzi allowed three runs and five hits in six innings, striking out seven and walking two.

Hanley Ramirez hit a homer in the fifth for Boston’s first run and added an RBI single in the sixth when the Red Sox scored twice to take a 3-1 lead.

NO-DECISION

Odorizzi didn’t want to discuss much about another strong start that fizzled to a no-decision, but he was thrilled to talk about the performance of his young catcher.

“Luke was the one who made it all count. What a clutch piece of hitting,” he said. “He got his pitch, he was sitting on it, and he made the most of it. Without that swing, we’re not back in the game.”

GREAT LOCATION

Boston took its first lead in the sixth after Dustin Pedroia walked with one out and went to third on Bogaerts’ double down the left-field line. Pedroia tagged up and scored on David Ortiz’s sinking fly ball to right, where Steven Souza Jr. prevented another run with a diving catch.

Boston ended up getting the run anyway on an odd single by Ramirez, whose towering popup to shallow right landed safely between Souza and first baseman Brad Miller.

“It’s one of those placement balls that found its spot,” Cash said.

STAY AWAY

Buchholz pitched just the eighth inning. Manager John Farrell said he needed to call on Buchholz for the second straight night — a first for the former starter — with reliever Brad Ziegler unavailable with an illness.

And with Boston in position for a return to the playoffs, the Red Sox weren’t taking any chances.

“He had to leave the ballpark,” Farrell said. “He has the flu so we’ve had to keep him quarantined.”

TRAINER’S ROOM

Rays: Tampa Bay enjoyed a second straight game without a player on the DL. The Rays got back to full strength Monday by activating RHP Alex Cobb, who is coming off Tommy John surgery and is scheduled to start Friday against Toronto. Before Monday, the Rays had at least one player on the DL every game since beating Minnesota 12-5 on July 6, 2011 — a string of 853 consecutive games.

Red Sox: OF Andrew Benintendi has been fitted for a brace for his sprained left knee. Farrell said it will take about a week to construct the custom brace that will allow the rookie to increase the intensity of his rehab workouts. Benintendi injured the knee last Wednesday in a loss at Tampa Bay.

UP NEXT

Rays: LHP Drew Smyly (6-11, 4.80 ERA) starts for the Rays in the series finale Wednesday afternoon. Smyly is 4-0 in his last seven starts with three no-decisions. Smyly has pitched at least six innings during the seven-game span.

Red Sox: RHP Steven Wright (13-6, 3.18) will make his first career start against the Rays. The knuckleballer took the loss Friday against Kansas City, allowing five runs and seven hits in six innings.