On any other night, the Yankees could be viewed as lucky to come back and beat the Mariners. After all, they botched two fly balls in the outfield and watched their most sure-handed infielder commit a costly fielding error.

Through eight innings Tuesday, they didn’t have a hit with runners in scoring position and were handcuffed by left-hander Marco Gonzales until a lengthy rain delay started in the top of the seventh.

Yet when the Mariners’ bullpen door opened and Anthony Swarzak walked through to protect a two-run lead in the ninth, luck went to sleep and opportunity surfaced.

Sure enough, the right-hander who was atrocious as a Yankee and as a Met was exactly what the Yankees’ dead bats needed.

Gio Urshela’s two-run homer to center field tied the score and Cameron Maybin singled off the right-hander, who gave way to lefty Roenis Elias to face Brett Gardner. Cameron Maybin swiped second before Gardner fanned, and DJ LeMahieu singled to right to score Maybin for a pulsating 5-4 win in front of an announced Yankee Stadium crowd of 36,851.

The Yankees had to wait out a short review process to resume celebrating a third straight victory that upped the record to 21-14.

“We didn’t play perfect, obviously, but we hung around enough,’’ manager Aaron Boone said of Gardner and Clint Frazier botching a routine fly ball in right-center, Frazier muffing a ball on the warning track in right-center and Urshela making a fielding error that led to two unearned runs in the eighth inning. “Gio, with two strikes, shows his power there. DJ does what DJ does.’’

Until the ninth, it appeared the Yankees were going to lose because of Urshela’s error, going 0-for-9 with runners in scoring position and failing to score in the seventh and eighth innings despite getting the first two batters on base.

Seeing Swarzak changed all that, and when he gave up a one-out single to Gleyber Torres, the Yankees had life. Swarzak got ahead of Urshela 0-2 and left a pitch in the middle of the plate that landed over the center-field fence.

“Fastball, hit that one,’’ a smiling Urshela said of his second homer of the season.

Maybin’s single to right and stolen base presented LeMahieu with a chance to win it.

“I wouldn’t have been in that situation without Gio,’’ LeMahieu said. “I was just trying to drive a ball up the middle.’’

By the time Maybin slid across the plate, Masahiro Tanaka’s 6¹/₃-inning stint that was ended by the 1-hour and 12-minute rain delay was a footnote. He gave up two runs, five hits, walked two and fanned four while working again without teeth in his signature splitter.

“It wasn’t as sharp as I wanted it to be,’’ Tanaka said.

Urshela, who bobbled an infielder grounder in the eighth when the Mariners scored twice, felt the Yankees had a chance in the ninth.

“When you get to that part of the game, everyone is anxious [because] we know we have a chance to win the game,’’ Urshela said.

Plucked from the Indians’ Triple-A team, Maybin has contributed since arriving.

“To come back in the ninth, ready to go without our heads down,’’ said Maybin, who is hitting .360 (9-for-25) in 10 games.

While it didn’t gain as much attention as Urshela’s homer or LeMahieu’s game-winning hit, Joe Harvey escaping a jam by stranding two in the ninth to keep the deficit at two was huge. He was rewarded with his first big-league victory.

Boone described the improbable win as a “fun one to be part of” and saved his best line to describe Urshela’s blast.

“On a damp night, go out the front door,’’ Boone said. “That was a bomb.’’

That it came against Swarzak in a New York ballpark wasn’t a shock.