The Wimbledon champion had struggled in the heat against Marton Fucsovics two days previously and been relieved to come through in four sets, but looked to be cruising in the slightly cooler evening conditions before things became complicated in the third set.
Djokovic fought back from a break down and held a match point at 5-4 but Sandgren saved it.
A sloppy tie-break from Djokovic coupled with inspired play from his opponent saw the American push the match into a fourth set, where the sixth seed regained his superiority to win 6-1 6-3 6-7 (2/7) 6-2.
Djokovic said: “Credit to Tennys for playing well, fighting, a great attitude on the court. I thought I played very well for the first two-and-a-half sets and then I just lost it mentally, I got pissed off at something, myself, my game, I don’t know.
“I’m not happy with the way I lost that concentration and composure there but I managed to regroup in the fourth. I started talking to myself, to my team, to everyone else. But it’s in these kind of moments you learn lessons and hopefully I’ll move on a better player.”
Novak Djokovic rolls into Round 3 with a 6-1, 6-3, 6-7, 6-2 win over Tennys Sandgren.
— US Open Tennis (@usopen) August 31, 2018
Djokovic’s Wimbledon title appeared to show he had put the mental and physical struggles of the past couple of years behind him, but the 31-year-old admitted there is still work to do alongside coach Marian Vajda.
“It’s a work in progress still,” he said. “We are working daily on trying to perfect the game and put it together. But we both still feel that I have lots of room for improvement. That’s what’s exciting about this sport.”
Djokovic next faces Frenchman Richard Gasquet, who defeated Laslo Djere 6-3 7-6 (7/5) 6-3.
Like Halep’s first-round loss to Kaia Kanepi, Wozniacki could have no complaints after being well beaten by Ukraine’s Lesia Tsurenko 6-4 6-2.
The Dane said: “I started off pretty well, then I think I played well in parts of the first set, just trying to stay aggressive. But she was playing smarter than me. She played the game that I was supposed to be playing.”
After breaking her grand slam duck in Melbourne after so many years of trying, it has not been a great season for Wozniacki, barring a title in Eastbourne in June.
She has failed to go beyond the fourth round at any of the three slams since, with this defeat following a second-round loss against Ekaterina Makarova at Wimbledon.
Wozniacki said: “This part of the season is usually a part of the season I really look forward to, one I really play well. I definitely would have wished to have played more and gone further. All I can do now is regroup, think what I can do, and also just make sure my body is 100 per cent.
“I’m always going to say it’s a great season because I won my first major. If I hadn’t won in Australia, we would be talking differently. But I did. I’m very proud of that. It’s something that nobody can ever take away from me.”
Over on Arthur Ashe, Maria Sharapova avoided more late-night drama, recovering from a break down in the second set to defeat Sorana Cirstea 6-2 7-5 and set up an exciting clash with Jelena Ostapenko.
No-one has risen more swiftly up the ranks in women’s tennis this season than Aryna Sabalenka, and the Belarusian is looking to do some damage in New York.
Sabalenka began the season ranked 78 but now sits just inside the top 20 and, at 20, is the youngest of that elite group.
After beating four top-10 players in a sequence of results building up to this tournament that culminated with her first WTA Tour title in New Haven, Sabalenka defeated Vera Zvonareva 6-3 7-6 (9/7) to reach the third round at a grand slam for the first time.
She said: “Of course this is unbelievable for me and I’m really in shock right now because it was such a great summer for me. I didn’t expect it to be that good and I’m so happy with this level I show on the court.”
The third round is usually where grand slams come alive and there will certainly be plenty of eyes on Sabalenka’s next clash with fifth seed Petra Kvitova, who is showing strong form in New York and defeated China’s Wang Yafan 7-5 6-3.
Wimbledon champion Angelique Kerber survived a lengthy tussle with Swede Johanna Larsson.
The German looked to be cruising at a set and a break up and served for the match, only for Larsson to hit back and take it to a decider, which fourth seed Kerber edged for a 6-2 5-7 6-4 triumph.
Sixth seed Caroline Garcia was also pushed hard in a 6-2 1-6 6-4 victory over Monica Puig while there were also wins for Naomi Osaka, Madison Keys and Kiki Bertens.
Eleventh seed Daria Kasatkina was beaten, though, the Wimbledon quarter-finalist falling 6-2 7-6 (7/3) to Aliaksandra Sasnovich.
Dominika Cibulkova defeated Hsieh Su-wei but was astonished to be penalised two points for arriving back late following the heat break at the end of the second set.
The Slovakian said: “It’s a 10-minute break, but they don’t consider that we walk from Court 17. It took me, I think, more than three minutes walking through the people, because there were so many fans.
“And then the umpire, she just told me, ‘OK, it’s going to be 0-30’, and I couldn’t believe it. I think it’s not right to get two-point penalties because of this.”
Serena and Venus Williams will go head-to-head for the 30th time on Friday after booking their spots in the third round of the US Open.
A potential clash between the sisters was the main talking point after the draw last week and neither looked like wobbling on Tuesday, with Venus defeating Camila Giorgi 6-4 7-5 before Serena saw off Carina Witthoeft 6-2 6-2 in the night session.
It will be the 16th time Venus and Serena have met at a slam, but the earliest stage of a tournament since their very first match in the second round of the Australian Open 20 years ago.
Their last slam clash was in the final of the Australian Open in 2017, when Serena was in the early stages of pregnancy with daughter Olympia.
Venus said: “The last time we played at the Australian Open it was two against one, so at least this time it will be fair.”
Venus won their most recent meeting at Indian Wells in March during Serena’s first tournament back, but the 36-year-old showed how much sharper she has got since then.
The Wimbledon finalist said: “It’s obviously a tough match. It’s so young in the tournament. It’s not the end of the world. We would have rather met later but we’re both going to come out per usual and do our best.
“We make each other better. We bring out the best when we play each other.
I think we’re used to it now.
“I never root against her, no matter what. So I think that’s the toughest part for me. When you always want someone to win, to have to beat them. I know it’s the same thing for her.”
Another blockbuster clash will see defending champion Sloane Stephens take on former world number one Victoria Azarenka, who has found her form in New York.
The Belarusian is playing at Flushing Meadows for the first time in three years, having missed 2015 while she was pregnant with son Leo and then last year because of a custody dispute that limited her travel.
Since a run to the semi-finals in Miami in March, when she was beaten by Stephens, Azarenka has played full-time on the tour but has struggled to find anything like the level that carried her to two Australian Open titles.
But the 29-year-old, whose ranking is languishing at 79, is feeling more positive now, saying: “I didn’t really enjoy myself because I feel like when you go through difficult times you sometimes have a very narrow focus on results only, and it’s hard to look outside of that.
“It didn’t bring me the joy of the journey, of experience. It really held me back from improving or transitioning from practice to match. It’s been really a struggle.
“But right now I feel happy on the court. I feel happy outside of the court. I feel that little by little, I am making that transition. I feel a lot more comfortable, confident on the court. I really do enjoy playing and fighting and just being here.
“If I look at the big picture and what I have been through and where I am now, it’s pretty significant. But it is not where I want to be. So that space sometimes seems really far.”
Stephens is defending a grand slam title for the first time and had to battle through her nerves and past qualifier Anhelina Kalinina, finding herself a set down and two games from defeat before turning things around to win 4-6 7-5 6-2.
“It was a tough day, obviously,” said the American. “She was playing really well, she’d played four matches already, she was used to the conditions.
“I just kind of had to weather the storm and wait for my opportunities. Obviously I wish I could have been playing a little bit better. That would have helped the situation. But I just found a way today, and sometimes it’s not going to be the best, but you’ve just got to battle through.”
With the clock having ticked past 1am, Czech qualifier Karolina Muchova knocked out 12th seed Garbine Muguruza, beating the former Wimbledon champion 3-6 6-4 6-4.