KANSAS CITY, Mo. — The Yankees got one they absolutely had to have.

Of course, in recent weeks, they’ve all felt that way to some degree.

Rallying from an early four-run deficit Wednesday night, then getting stellar work from their taxed bullpen, the Yankees pushed one across in the 13th inning to take a 5-4 victory over the Royals in front of 22,615 at Kauffman Stadium.

“I feel like we’ve been playing do-or-die for the last month here,” said Chase Headley, who had two hits and a walk.

The Yankees (69-63), who completed a 17-11 August, are 2½ games behind the Orioles and Tigers, who are tied for the second AL wild-card spot.

The Bombers took two of three from the Royals and, after an off day Thursday, start a three-game series against the Orioles in Baltimore Friday.

“The opportunity’s in front of us,” said Joe Girardi, who got seven scoreless innings from six relievers, the vast majority of whom he didn’t have in his bullpen a month ago. “We have a hill to climb but I really believe in these guys.”

advertisement | advertise on newsday

The Royals (69-64), who had won 18 of their last 23 coming in, fell ½ game behind the Yankees.

Royals righty reliever Chris Young, who allowed four runs Monday night and did not retire a batter, brought that level of execution into Wednesday.

He escaped a bases-loaded, one-out jam in the 12th but was not as fortunate in the 13th.

Didi Gregorius opened the latter inning with an infield single and Starlin Castro, who hit a two-run homer in the sixth, doubled.

In came lefty Matt Strahm, the Royals’ seventh reliever, to face Brian McCann, whose sacrifice fly made it 5-4.

Yankees videos

Dellin Betances, who worked two scoreless innings in Tuesday night’s 5-4 victory in 10 innings, walked Cheslor Cuthbert to start the bottom of the 13th, but got a double play off the bat of Eric Hosmer — Betances fielded the grounder and started a somewhat awkward looking 1-6-3 double play — and a fly out from Kendrys Morales to end it for his seventh save. Betances was part of a group of relievers that did not allow a hit over the seven innings.

“The guys were tremendous, the whole bullpen,” Betances said. “If you give up a run, this game is over.”

After the Yankees squandered a base-loaded, one-out situation against Young, rookie Ben Heller, who picked up his first career win, pitched a 1-2-3 bottom of the 12th. He was preceded to the mound by Blake Parker, Adam Warren, Tommy Layne and Chasen Shreve.

“You build off the momentum,” Warren said of the relievers. “Shrever set the tone for us. Everybody was kind of calm under pressure.”

Righthander Luis Cessa was so-so in his third big-league start. The rookie allowed four runs (three earned) and six hits over six innings. Two of the hits were homers, a two-run job by Morales that made it 2-0 in the first and a solo shot by Hosmer in third that made it 4-0.

advertisement | advertise on newsday

Royals starter Ian Kennedy, who came in 9-9 with a 3.57 ERA, including 3-0 with a 1.14 ERA in his previous six starts, pitched five shutout innings before losing his command. The former Yankee could not hold the 4-0 lead he was given, allowing three runs in the sixth — Castro’s two-run homer made it 4-3 — and one in the seventh on and Ellsbury sacrifice fly.

“We still believe that we can win in here and these guys are showing it,” said Girardi, whose club had the kind of month few predicted at the trade deadline when Aroldis Chapman, Andrew Miller and Carlos Beltran were dealt. “They just keep fighting.”