ST. PETERSBURG — Closer Bryan Baker recorded the first five-out save of his career — his Major League-leading 35th of the season — as the Tampa Bay Rays beat the Baltimore Orioles 7-6 at Tropicana Field to avoid a four-game home sweep.

The victory improved the Rays to 75-49 and extended their lead over the Yankees for the top spot in the American League to six games. Baker, who has now converted a franchise-record 24 consecutive save opportunities, entered in the eighth inning with runners on first and third and one out to protect a one-run lead.

“You never want to get swept, especially for four games at home, in-division,” Baker said. “They’re just playing really good baseball right now, so to be able to scrap and claw back and come out of here with a win tonight was really big for us.”

Manager Kevin Cash had set the tone before the game. “We need to find a way to win a game,” Cash said. “The Orioles have played very well against us, and [I] feel like we need to get back to doing our thing.”

The Rays trailed by two runs heading into the bottom of the eighth before a rally that featured contributions from across the roster. Pinch-hitter Taylor Walls reached on a single that deflected off first baseman Coby Mayo’s glove, and Yennier Cano hit Yandy Díaz with a pitch. Jonathan Aranda lined an RBI single to left field, and Junior Caminero loaded the bases with a swinging-bunt single, prompting the Orioles to bring in lefty Josh Walker.

Cash countered by sending pinch-hitter Jonny DeLuca to the plate, and DeLuca chopped a changeup over the infield for a go-ahead, two-run single. “Had kind of a tough weekend … so just trying to put the ball in play in a situation like that,” said DeLuca, who had been hitless in his first 10 at-bats of the series. “Ended up not being so pretty, but working out.”

Earlier, Ryan Vilade and Jorge Mateo had hit homers to erase a 3-1 deficit, though former Baltimore teammate Tyler Wells surrendered a pair of Orioles homers in the fifth. Setup man Kevin Kelly worked a scoreless sixth despite allowing two singles, and lefty Cam Booser struck out three in the seventh to keep the deficit at two.

Baker struck out Alonso and induced an inning-ending grounder from Jackson Holliday in the eighth, then confirmed with pitching coach Kyle Snyder that he was ready for the ninth. “I figured I knew his answer was going to be that he was staying in the game,” Cash said, “but appreciate the heck out of it.” Baker worked around a two-out single in the ninth to seal it.

“The guys stayed at it, didn’t get frustrated,” Cash said. “I think that’s probably a byproduct of knowing that we’re a good team and we’ve kind of withstood some stuff in the past, and they just stayed at it.”

DeLuca offered the simplest assessment of Baker’s dominance: “It’s just special every single time. I feel like he’s got it on lock. It’s really hard to do, and he makes it look really easy.”

Baker, who had pitched only once since Aug. 8, said the timing worked in his favor. “That was a different kind of thrill. We really wanted to win that game,” Baker said. “Didn’t want to leave that series with the sweep, so pretty excited about that one.”