NEW YORK — The natural order of tennis things was upended Sunday night when No. 1 Novak Djokovic finished a meek third game of a third set, hugged his friend and opponent Stan Wawrinka, retired from their fourth-round match and left both a disappointed ­Arthur Ashe Stadium and a ­redefined U.S. Open.

The pain flaring from Djokovic’s left shoulder, an issue through much of his truncated stay here, had loomed while Wawrinka blasted to a 6-4, 7-5, 2-1 lead, looking occasionally like the destroyer who won three Grand Slam tournaments ­mid-decade. When it ended abruptly, Wawrinka had his fourth Grand Slam win over Djokovic (including two finals), his fifth win over a No. 1 player (tied for second in history) and the loudest peak of his long and winding return from knee surgery in 2017 and a ranking of 263 in 2018.

Djokovic exited to boos and then scattered cheers from a crowd that mostly rooted against him. “The pain was constant for weeks now,” he said. “Some days higher, some days with less intensity and obviously taking different stuff to kill the pain instantly. Sometimes it works; sometimes it doesn’t.”

As he slogged through 1 hour 46 minutes, he looked slightly but chronically less capable than Wawrinka, even as Djokovic’s early break in the second set supplied a 2-0 lead that grew to 4-1 and made it seem as if he might restore order. Instead, he lost service twice in the set thereafter, leaving clues of ­increasing ­desperation with drop shots that fluttered into the net and even a moment late as Wawrinka served at 4-5 when Djokovic halted a point to hope for a missed line call.

Video technology upheld that Wawrinka’s shot had landed on the line, and Djokovic lost the point. Soon, the tournament lost the 32-year-old No. 1 seed who had won four of the past five Grand Slam tournaments, including this year’s Wimbledon in a donnybrook with Roger Federer and this year’s Australian Open in a masterful rout of Rafael Nadal.

Those two victims probably bolted immediately to the minds of fans after Djokovic’s sixth career retirement in a Grand Slam but only his second since 2009. If Federer and Nadal wind up meeting here in the final Sunday, it would snuff a longtime quirk in the sport and become their first U.S. Open meeting after four meetings in Melbourne, six in Paris at the French Open and four at Wimbledon.

It will be the 34-year-old Swiss Wawrinka who will oppose the hottest player of the ATP Tour summer, No. 5 Daniil Medvedev of Russia, in a quarterfinal, and it figures to mark Wawrinka’s second straight round as a crowd favorite. The last two rounds for Medvedev, a 23-year-old with an occasional temper, have featured boisterous set-tos with Louis Armstrong Stadium audiences. The winner of Wawrinka-Medvedev will play the winner of Federer’s quarterfinal with Grigor Dimitrov, the 28-year-old Bulgarian who eased through the rising Australian Alex de Minaur, 7-5, 6-3, 6-4.

“Sorry he had to retire to finish the game like that, but for me, most important is the way I’m playing, the way I’m moving,” said Wawrinka, perched at No. 24 in the rankings after his trip to the depths. The 2014 Australian Open, 2015 French Open and 2016 U.S. Open champion added, “Tonight I think was really high level.”

He also said, “I was hitting really hard the ball” — which proved utterly true late in the second set as Djokovic strained for ways to get points and Wawrinka built confidence. He had just 19 unforced errors to Djokovic’s 35 and won 11 of his 12 service games. His one-handed backhand often stirred gasps again.

“Look, I congratulate Stan,” Djokovic said, soon calling Wawrinka “a big-matches player. He loves to play on the big stage against the best players of the world. It’s what got him three Slams and Olympic gold and everything.”

It’s also part of what got Djokovic to a whopping 16 Grand Slams, just below Nadal’s 18 and Federer’s 20, but something seemed imperfect here even as Djokovic generally declined to discuss it. In wins over Roberto Carballes Baena, Juan Ignacio Londero and Denis Kudla and then again soon before retiring Sunday night, Djokovic often took visits from the trainer to tend to his left shoulder. He would sit down, and his shirt would come off.

After a third game of the third set in which he looked lost, he made the formalities of retiring and then made for the exit. Boos came, and Djokovic said: “Look, I’m not being offended by, you know, mistreated by anybody. I don’t really, you know, pay too much attention to that. You know, I like to respect others. I hope that others can respect me and my decision. I’m sorry for the crowd. Obviously they came to see a full match, and just wasn’t to be. That’s all it is.”