KANSAS CITY, Mo. — This was the great escape and it wasn’t entirely clear the Yankees deserved it.

But they will more than take it.

And their flickering playoff hopes certainly needed it.

On a crazy night of baseball, which included a 59-minute rain delay that forced an effective Masahiro Tanaka to the showers early, the Yankees got past the Royals, 5-4, in 10 innings at Kauffman Stadium late Tuesday night.

The Yankees (68-63), who got a two-run homer from slumping rookie Aaron Judge in a three-run second inning but otherwise spent the majority of the night stranding runners (13 of them), stayed 3 1⁄2 games behind the Orioles for the AL’s second wild-card spot.

The Royals (69-63), who loaded the bases with one out in the bottom of the 10th but could not score against lefthander Chasen Shreve, lost for just the fifth time in their last 23 games.

advertisement | advertise on newsday

“Really a tough situation he came into and he really picked us up,” Girardi said of Shreve, recalled from Triple-A earlier in the day.

Shreve, who has struggled all season, cleaned up the bases-loaded mess he inherited from rookie righty Ben Heller, whom Girardi called on for the 10th to protect a one-run lead.

“I was trying to keep my emotions at bay,” Shreve said.

Dellin Betances (3-4) had already pitched the eighth and ninth innings — the righthander’s first two-inning outing of the season — and the manager was not tempted to put his closer out for a third inning.

“I’m not going to risk his health,” Girardi said. “It’s just not right.”

Yankees videos

A jittery Heller, one of four prospects acquired from the Indians in the Andrew Miller deal, hit the first batter he faced, Raul Mondesi, who stole second. Leadoff man Jarrod Dyson lined a single to center, putting runners on the corners for Lorenzo Cain. The rightfielder struck out and, after Eric Hosmer was intentionally walked to load the bases, Shreve came on to face Kendrys Morales, who homered in the fourth off Tanaka and drove in a run with a sacrifice fly in the eighth. Shreve struck out Morales, the Royals’ RBI leader with 61, on three pitches, then got Salvador Perez, second to Morales with 58 RBIs, to fly to center to end it.

“I want to be that guy,” Shreve said of pitching important innings, as he did much of 2015 before imploding the final three weeks, inconsistency he carried into this season. “I’ve been looking for something to get me going.”

The winning rally came in the 10th against Joakim Soria (4-6), the Royals’ sixth pitcher of the night, who allowed back-to-back singles to Brian McCann and Chase Headley. Aaron Hicks pinch ran for McCann and went to third when Brett Gardner, who reached base five times, walked with two outs to load the bases. Jacoby Ellsbury then collected his fourth hit of the night, a slow bouncer back up the middle that deflected off Soria for a single that pushed Hicks across to make it 5-4.

“You need breaks throughout the season,” Ellsbury said. “I guess it was a break for us.”

Tanaka, 4-0 with a 1.63 ERA in his previous four starts, pitched reasonably well, allowing two runs and four hits in five innings.

advertisement | advertise on newsday

But with the Yankees ahead 4-2 going into the top of the sixth, heavy rain caused a 59-minute delay and ended Tanaka’s night after 71 pitches.

After the delay, Royals manager Ned Yost, who got just 3 1⁄3 innings out of starter Edinson Volquez, brought on former Yankee Chien-Ming Wang. The righthander escaped a jam after Tyler Austin led off with a walk and Gardner doubled. It was, on this night, par for the course as the Yankees went 2-for-16 with runners in scoring position.

Girardi said had the Yankees taken the loss, it would have been “really difficult” to swallow.

“We had a lot of opportunities that we didn’t cash in on tonight,” Girardi said. “Pitchers need to pick up the hitters sometimes.”

That was more than the case with Shreve.

advertisement | advertise on newsday

“That was,” Tanaka said through his translator of the jam Shreve got out of, “spectacular.”