BALTIMORE — J.J. Hardy and Michael Bourn came through with RBIs in the eighth inning, and the Baltimore Orioles rallied from a four-run deficit to beat the Tampa Bay Rays 5-4 on Friday night.

The final out came when Mike Mahtook was cut down at the plate trying to score from first base on a single into the left-field corner by Alexi Ramirez.

Pedro Alvarez and Chris Davis homered for the Orioles, who began the day two games behind first-place Boston in the AL East and tied with Toronto for the two AL wild cards.

Down 4-0 in the second inning, Baltimore closed to 4-3 before completing the comeback in the eighth against wild reliever Brad Boxberger (4-2).

Two walks and a hit batter loaded the bases for 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 Zach Britton got three outs for his 44th save in 44 tries.

advertisement | advertise on newsday

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.

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.

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

But Baltimore would not be denied in the eighth.

Baseball videos

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.

Jimenez settled down after that. The right-hander gave up four runs over seven innings, striking out eight.

Alvarez hit his 21st home run in the second and 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

advertisement | advertise on newsday

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, Rays manager Kevin 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.

advertisement | advertise on newsday

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. Rickard has been on the DL since July 21.

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.