Brett Gardner drove in three runs for the Yankees, two on a sharp infield single in the 11th that made it 8-5.
But the Royals, who tied the score in each of the previous four innings, once again refused to go down easily. They got a two-out RBI single from pinch-hitter Edward Olivares and brought the potential winning run to the plate before Carlos Santana grounded out to end a game that took 4 hours, 52 minutes.
"I'm just proud that, on a scorching-hot night, they just kept coming," Yankees manager Aaron Boone said.
Wandy Peralta earned his third major league save - the first two came with San Francisco this season.
Luke Voit, activated from the injured list Sunday, snapped a scoreless tie with an RBI single in the seventh and hit a tiebreaking homer in the ninth for New York. Aaron Judge had a run-scoring single in the eighth to make it 2-1.
"That was a grind," Voit said, "but it was the definition of a team win. Everybody on the lineup card got the job done. Obviously, we had some ups and downs in the game, but we kept fighting back."
Kansas City became the first team since 1900 to erase a deficit in the seventh, eighth, ninth and 10th innings of a game, according to STATS.
"I will not tire of talking about how hard these guys play the game and how great of a job they do fighting back all the time," manager Mike Matheny said. "It's unique, and the sign of professionals. Unfortunately, we got too big of a gap there in that last (inning)."
The Yankees got single runs in the seventh, eighth and ninth, but the Royals kept answering. Ryan O'Hearn's sacrifice fly in the seventh, Andrew Benintendi's single in the eighth and Nicky Lopez's single in the ninth each tied the score.
New York pushed across two in the 10th on a sacrifice fly by Kyle Higashioka and a single by Gardner. Kansas City evened it once again on a sac fly by Jarrod Dyson and a single by Hanser Alberto.
Clay Holmes (4-2) got the win. Greg Holland (2-5) took the loss.
Giancarlo Stanton's 122 mph double-play grounder in the first inning matched his own mark for the fastest exit velocity of a ball put in play in the StatCast era.
Neither starter got a decision. Kansas City's Carlos Hernandez allowed one run and five hits in 6 2/3 innings. He walked one and struck out three. Jameson Taillon gave up an unearned run and four hits in six-plus innings. He struck out four and walked two.
Play was delayed in the top of the fourth when plate umpire Brian Knight left the game. Pat Hoberg, who had been at third base, moved behind the plate. No reason was given for Knight's departure.
Yankees INF Gleyber Torres was placed on the 10-day injured list with a left thumb sprain. Boone said the team "got good news (Sunday night). We were concerned that it was going to be more serious. As of right now it's a sprain. The initial thought is 10 to 20 days."
Torres' spot on the roster was taken by INF Andrew Velazquez, who was brought up from Triple-A Scranton/Wilkes-Barre. The Yankees also recalled RHP Nick Nelson from Triple-A to take the spot of replacement player RHP Luis Gil, who was returned to Scranton/Wilkes-Barre after Sunday's game.
The Yankees send LHP Nestor Cortes Jr. (0-0, 2.15 ERA) to the mound for the second game of the series Tuesday night. Cortes will make his fifth start and 13th appearance for New York in 2021. He's faced the Royals twice in his career - both in relief - and tossed 6 2/3 scoreless innings.
Kansas City counters with Daniel Lynch (2-3, 6.00). He's 2-1 with a 1.89 ERA since being recalled from Triple-A Omaha on July 25.
Boone ejected after Loaisiga called for balk
Jonathan Loaisiga is called for a balk, and a furious Aaron Boone gets ejected. The Royals would tie the game on a sac fly later in the at-bat.