A daring, and perhaps ill-advised, fifth-inning steal of home by Jacoby Ellsbury produced the tying run in Friday night’s game, which the Yankees eventually won.

On Saturday, Ellsbury reached on a critical catcher’s interference that contributed to a game-tying rally in an odd seventh inning.

Hey, the way things have been going for the Yankees’ offense, whatever it takes.

The seventh inning, which featured an unusual combination of two four-pitch walks, the catcher’s interference and Brett Gardner’s soft liner off pitcher Xavier Cedeño’s glove for an RBI infield single, made the Yankees’ walk-off victory in the bottom of the ninth possible.

That’s when they scored in more traditional fashion. With two outs, Gardner’s solo home run into the second deck in rightfield off Erasmo Ramirez gave the Yankees a 3-2 victory over the Rays in front of 40,714 at the Stadium.

“I got a good pitch to hit,” Gardner said of the fat 3-and-1 fastball that resulted in his second homer of the season.

advertisement | advertise on newsday

The victory was the second straight for the Yankees (7-9), who could hardly be described as having broken out of their offensive malaise after losing three straight and seven of eight.

“We haven’t played our best baseball, but we’re a really good ballclub from top to bottom,” Brian McCann said. “We’ve got a lot of experience in here. You want to play good baseball, and we’re working our way toward that.”

The Rays (7-10) all but handed things to the Yankees in the seventh. Lefthander Enny Romero walked McCann, and after Starlin Castro flied to center, Cedeño, another lefthander, walked Chase Headley.

Cedeño fell behind Didi Gregorius 2-and-0, but the shortstop then swung at a pitch that appeared out of the strike zone and eventually struck out swinging on a full-count curveball that was out of the strike zone.

With the count 2-and-2, Ellsbury swung and missed but clipped the glove of catcher Curt Casali and was awarded first on catcher’s interference, loading the bases. It was the third catcher’s interference Ellsbury has drawn this season.

Yankees videos

Gardner then lined one off Cedeño’s glove — “I didn’t hit it that hard; it was off the end of the bat and I was happy when I saw it deflect off him,” he said — to tie it at 2-2.

“It’s a little different, not how you would draw it up, but at this point, we’ll take it any way we can get it,” Gardner said of the inning. “Some guys came through with some big at-bats.”

The Yankees’ work on the mound was exemplary, from starter to back end of the bullpen. Masahiro Tanaka turned in his best outing of the season, allowing two runs and five hits in seven innings to lower his ERA to 2.92. He walked one and struck out a season-high seven.

Tanaka wasn’t as high on his performance as everyone else was, though. “I really couldn’t find anything that was working perfect for me,” he said through his translator. “But I think the key was that I was able to hang in there in key moments.”

Before the seventh, Tanaka looked as if he might take the loss.

advertisement | advertise on newsday

The Rays tied the score at 1-1 in the fourth when with two outs, Corey Dickerson hit a long RBI double to left on an 0-and-2 fastball. They took a 2-1 lead in the fifth when Kevin Kiermaier turned on an 0-and-1 fastball and hammered it off the rightfield foul pole.

Lefthander Blake Snell, who was brought up Saturday to spot-start and is scheduled to be sent back to the minors Sunday, was terrific in his five innings. The 23-year-old, who is the Rays’ top pitching prospect, shook off a rough first inning and allowed one run and two hits in five innings. Snell, with a mid-90s fastball and diving curveball among the pitches he had working, walked one and struck out six.

Dellin Betances struck out two of three in the eighth, giving the righthander 21 strikeouts in his last 24 batters faced, and Andrew Miller (1-0) pitched a scoreless ninth.

“When you use Miller and Betances, you want to win those games,” McCann said. “Gardy came up with the huge hit.”

Two of them, in fact.