BALTIMORE — The Tampa Bay Rays apparently have no intention of sleepwalking to the finish of a disappointing season.

Although the Rays have been eliminated from playoff contention, they gave the Baltimore Orioles all they could handle Friday night before falling 5-4.

Unlike Tampa Bay, Baltimore is in full-blown playoff mode. The Orioles are locked in a duel for the top spot in the AL East and competing for, at worst, one of the two wild cards.

Tampa Bay went up 4-0 in the second inning and fell behind 5-4 in the eighth before nearly pulling even in the ninth against Baltimore’s perfect closer, Zach Britton.

With two outs, Mike Mahtook singled off the second base bag. Alexei Ramirez followed with a liner to left, and Mahtook wheeled toward home. The relay from Michael Bourn to third baseman Manny Machado to catcher Matt Wieters easily beat him there.

“I knew it was going to be close, just based on the way Alexi hit the ball,” Mahtook said. “Left-field line, it wasn’t like he had super long way to run for it. I was on the move, he made a good play, and they were able to get me.”

Evan Longoria hit his career-high 34th homer and Richie Shaffer also connected for the last-place Rays.

Tampa Bay starter Chris Archer allowed three runs in 6 1/3 innings and struck out four to increase his AL-leading total to 221. The 18-game loser also avoided setting a franchise record for losses in a season.

“Can’t say I’m pleased with the way things went,” the right-hander said. “We lost the game.”

Archer was replaced after giving up successive one-out singles in the seventh. Danny Farquhar hit Adam Jones with a pitch to load the bases for Hyun Soo Kim, who hit a deep liner to center. Kevin Kiermaier leaped with his back to the 7-foot wall, grasped the ball and brought it back onto the field before it popped from his glove.

“That’s a grand slam with any other center fielder,” Rays manager Kevin Cash said.

Kim was limited to an RBI single, and Farquhar struck out Machado and Davis.

But Baltimore would not be denied in the eighth against wild reliever Brad Boxberger (4-2).

Two walks and a hit batter loaded the bases for J.J. Hardy, who tied it with an infield single. Bourn followed with a fly ball to left field, and pinch-runner Nolan Reimold easily scored as the throw was cut off.

Brad Brach (9-3) worked the eighth and Britton escaped with his 44th save in 44 tries.

“If we get to where we’re looking for,” Orioles starter Ubaldo Jimenez said, “it’s one of those games that you can point out: What was the game that made the difference?”

Longoria homered off Ubaldo Jimenez with a man on in the first inning and Shaffer went deep in the second for a 4-0 lead.

Pedro Alvarez hit his 21st home run in the second for Baltimore and Chris Davis delivered No. 38 in the fourth. Davis also struck out twice and has 201 for the year.

Baltimore’s Mark Trumbo, the major league home run leader with 42, was sidelined with back spasms. It was only the second game he’s missed this season.

OUT IN LEFT FIELD

Brad Miller usually plays shortstop or first base for the Rays. In an effort to gauge Miller’s versatility and add flexibility to the roster, Cash started him in left field.

It didn’t take long for Miller to get noticed: The second Baltimore batter of the game hit a pop to left that glanced off Miller’s glove for an error.

The glove was borrowed from Farquhar.

TRAINER’S ROOM

Rays: 1B Nick Franklin, who strained his hamstring on Thursday, was held from the game. Cash listed him day-to-day.

Orioles: RHP Darren O’Day (rotator cuff strain) threw 26 pitches in a simulated game and could be activated Sunday or Monday. … OF Joey Rickard (thumb) took a couple of practice swings, and “it did not go well,” manager Buck Showalter said.

UP NEXT

Rays: Matt Adriese (7-7, 4.46 ERA) starts Saturday night, coming off his first career win against the Yankees on Sunday. The right-hander is 0-3 with a 9.24 ERA lifetime vs. Baltimore.

Orioles: Chris Tillman (16-5, 3.68) makes his second start since being activated from the DL with right shoulder bursitis. He beat Detroit on Sunday.