BOSTON — Boston’s late lead unraveled along with a great chance for the Red Sox to move within a game of first place in the AL East.

The last-place Tampa Bay Rays kept Boston right where it started the night, rallying from a 3-1 deficit to win 4-3 Tuesday on Evan Longoria’s solo home run in the eighth.

“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.”

The Red Sox failed to capitalize on Toronto’s 5-3 loss to Baltimore. Boston remained two games behind Toronto for the AL East lead and the Orioles just one game behind the Red Sox.

Longoria’s 31st homer of the season cleared Fenway Park’s “Green Monster” and completed a rally catcher Luke Maile began the previous inning with a two-run homer to left, tying the game at 3-all. It was the second career homer for Maile, who drove a 1-2 curveball out to left and ended what had been a strong start for Drew Pomeranz.

Pomeranz struck out eight in 6 2/3 innings and was just one strike away from cruising into the eighth with a two-run lead.

“This one’s on me. it’s a tough way to lose,” Pomeranz said.

Pomeranz and Tampa Bay starter Jake Odorizzi both pitched well but didn’t factor in the decision as both bullpens struggled to hold a lead.

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.

Former starter Clay Buchholz (5-10) took the loss, pitching the eighth and allowing Longoria’s solo shot.

Hanley Ramirez hit a homer in the fifth for Boston’s first run and added an RBI single in the sixth when Boston scored twice to take a 3-1 lead. David Ortiz drove in a run with a sacrifice fly in the sixth.

UP HIGH

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. His last pitch was a curveball he said he intended to bounce, but left high in the zone for Maile.

“I don’t miss much with my curveball. I have pretty good control with it,” Pomeranz said. “I just left it up a little bit too much and he got it. That’s all there is to say about it.”

BOGAERTS MILESTONE

Bogaerts reached 500 career hits with his double in the sixth. At 23 years old, he joined Ted Williams, Bobby Doerr, Tris Speaker and Tony Conigliaro as the players in club history to reach 500 hits before turning 24.

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. Manager John Farrell 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.