BALTIMORE — Thrust into the unwanted position of playing out the final weeks of a losing season, the Tampa Bay Rays have found a way to make the situation more tolerable.

Evan Longoria homered and drove in five runs, and the last-place Rays again played spoiler in a 7-6 victory over the Baltimore Orioles on Thursday night.

The loss dropped the Orioles two games behind AL East-leading Boston. They fell into a tie with Toronto for the two AL wild cards when the Blue Jays beat the Los Angeles Angels later Thursday.

That win by Toronto mathematically eliminated Tampa Bay from playoff contention, but the Rays have won three straight — including the last two of a three-game series in Toronto. They also defeated the wild card-contending Yankees on Sunday.

“It’s fun that we are playing competitive baseball,” Longoria said. “It’s one thing to go out in a great environment against a team that is a playoff contender, but to be in the game and winning games is the best thing about it.”

The Rays aren’t embracing the role of spoiler. They’re just trying to go out on a high note, and if it hurts their opponent, well so be it.

“We’re playing well and this is great learning experience for all of us in these environments,” manager Kevin Cash said.

Longoria said: “I don’t think we’re coming to the ballpark every day thinking we want to spoil this team or that team’s chances of getting to the playoffs.”

Alex Colome worked the ninth for his 33rd save. After the Orioles got runners at first and third with one out, the right-hander struck out major league home run leader Mark Trumbo and slugger Chris Davis.

Baltimore went 3 for 15 with runners in scoring position.

Although the temperature was a comfortable 72 degrees and the Orioles are in the midst of a playoff push, the game attracted an announced crowd of only 19,233.

Longoria put the Rays up 3-0 before Yovani Gallardo (5-8) got an out. Baltimore answered in the bottom half with a two-run single by Matt Wieters and a two-run double by J.J. Hardy.

Tampa Bay pulled even in the second, went ahead in the third and used Longoria’s two-run single to make it 7-4 in the fourth.

When Orioles manager Buck Showalter emerged from the dugout to pull Gallardo in the fourth, many of the hometown fans cheered. They subsequently booed the right-hander as he walked toward the dugout.

Gallardo gave up six runs and seven hits in 3 1/3 innings. In 21 starts this season, he’s surrendered 21 earned runs in the first inning.

Baltimore’s Nolan Reimold hit into a double play with runners on the corners in the fourth. The Rays worked out of an even bigger jam in the fifth, retiring Manny Machado, Trumbo and Davis with runners at second and third.

Brad Boxberger (4-1) got the last two outs in the fifth.

Run-scoring groundouts by Hardy and Michael Bourn made it 7-6 in the eighth.

TRAINER’S ROOM

Rays: 1B Nick Franklin left in the fifth inning with a left hamstring strain. … 1B Logan Morrison will get a second opinion on whether surgery is required for his season-ending left wrist injury. Morrison was injured while taking a swing Sunday and received an MRI this week.

Orioles: OF Steve Pearce will be sidelined until the final week of the season, maybe longer, after receiving a platelet-rich plasma injection in his throwing arm Wednesday. … RHP Darren O’Day (rotator cuff strain) will throw a simulated game Friday.

UP NEXT

Rays: Chris Archer (8-18, 4.05 ERA) starts Friday night, seeking to pad his AL-leading strikeout total of 217. He also hopes to avoid setting the franchise record for losses in a season. The right-hander is currently tied with Tanyon Sturtze, who lost 18 in 2002.

Orioles: RHP Ubaldo Jimenez (7-11, 5.98) strives to win his fourth straight start. He’s got a 2.83 ERA in his last four outings.