KANSAS CITY, Mo. — Joakim Soria threw the pitch he wanted to Jacoby Ellsbury with two outs in the 10th inning Tuesday night, and even got the result he wanted — right until the ball bounced away from him.

The grounder off Soria scooted toward the left side of the infield for a base hit, allowing the go-ahead run to score and helping the New York Yankees to a 5-4 victory over the Kansas City Royals.

“That was an easy ground ball right to me. I don’t know if I slipped or something, but I ended up on the ground,” said Soria (4-6), who squandered a comeback from a four-run deficit. “It’s frustrating.”

The Royals nearly pulled off another comeback.

Dellin Betances (3-4) tossed two scoreless innings of relief for New York before Ben Heller loaded the bases with one out in the 10th. Chasen Shreve entered the game and calmly struck out Kendrys Morales and got Salvador Perez to fly out, picking up his first career major league save.

“I mean, it was really difficult,” Yankees manager Joe Girardi said of the mess Shreve inherited, which led to his first career major league save. “He came in and really picked us up.”

Aaron Judge hit a two-run homer for the Yankees, while Ellsbury finished with two RBIs.

Morales went deep for the Royals to spur their comeback, then provided the tying sacrifice fly in the eighth inning. Jarrod Dyson and Lorenzo Cain also drove in runs for Kansas City.

“What these guys did, they fought to the bitter end,” Royals manager Ned Yost said. “We were down 4-0, battled back, tied the ball game under horrible conditions. It just wouldn’t stop raining from the third inning on. We had the go-ahead run on second base. We just couldn’t get it in.”

The Yankees built a 4-0 lead off Edinson Volquez by the third inning, only to watch it slowly slip away around a 59-minute rain delay that saturated the soggy turf at Kauffman Stadium.

In fact, just about all that could slow down the Yankees’ Masahiro Tanaka was the rain.

The right-hander mowed through the first eight batters he faced, extending the streak of 14 2/3 scoreless innings he had twirled in his last two starts. Tanaka didn’t allow a hit until a single by Raul Mondesi, who later scored on Dyson’s triple off the top of the wall.

Tanaka’s only other mistake came on Morales’ homer in the fourth inning.

The rain began falling and the tarp came out after the fifth inning, and the delay was long enough to end Tanaka’s night. He allowed two runs and four hits while striking out four without a walk.

“It’s tough. I had enough energy so definitely I would have liked to go out there,” Tanaka said through a translator, “but you can’t do anything about the weather.”

The Royals got within 4-3 in the sixth inning when Cain slapped an RBI double off Adam Warren, then they coaxed across the tying run in the eighth against Tyler Clippard and Betances.

Clippard walked Cain to start the inning. Cain promptly stole second off Betances, and the throw from catcher Gary Sanchez squirted into the outfield, allowing Cain to reach third. He trotted home to knot the game 4-all when Morales lofted a sacrifice fly to center.

“That’s a game where you battle your tail off. You end up don’t winning, so OK — you come back tomorrow and find a way to win,” Yost said. “It’s as simple as that.”

ROSTER MOVES

The Yankees optioned RHP Kirby Yates to rookie-level Pulaski and recalled Shreve from Triple-A Scranton/Wilkes-Barre. Shreve joined the Yankees for the fourth time this season.

TRAINER’S ROOM

Yankees: 1B Mark Teixeira sat again with pain in his neck, Girardi said. “From a health standpoint I have to watch him closely, but he’s feeling better today than he was yesterday.”

Royals: RHP Wade Davis allowed one hit and struck out two in one inning at Triple-A Omaha. Davis is on a rehab assignment after a flexor strain but could rejoin the Royals soon.

UP NEXT

RHP Luis Cessa, the second Yankees pitcher since 2004 to win his first two career starts, returns to the mound for New York. RHP Ian Kennedy tries for his fourth straight win for the Royals.