The French Open champion was taken the distance by Lorenzo Sonego in his opening contest and came under pressure from Halys throughout the next, forcing him to summon his resilience again and stay on track for a maiden title in New York.
"The performance was quite shit to be honest, but I won and that's the most important thing," the 29-year-old world No.2 said following his marathon battle that lasted four hours 33 minutes and finished at 2.15am local time.
"That's what I go to the gym for, that's what I go to the track for, what I train for, to be able to come out on top in these difficult moments.
"I hope at some point I'll start playing better, but for now I'm happy to be in the third round."
Zverev, the 2020 runner-up competing as the top seed at a major for the first time in his career, took the opening set on the back of a solitary break, but the world No.52 levelled the contest, bringing the Arthur Ashe Stadium crowd to life.
After forcing a tiebreak and claiming the third set to edge ahead in the contest, Zverev dropped the fourth, before building a 4-2 advantage in the decider.
Zverev raised his level another notch and earned a hard-fought win for a clash with 25th seed Alejandro Tabilo in the next round.
The 25th-seeded Chilean defeated Australia's Alexei Popyrin 2-6 7-6 (7-4) 7-6 (7-3) 6-2 earlier on Thursday (Friday AEST).
Karen Khachanov stunned third seed Felix Auger-Aliassime, capitalising as the 2025 semi-finalist struggled to deliver his best on Louis Armstrong Stadium.
The world No.50, who lost the opening match in his three Open tune-up events, did enough on a day when his Canadian opponent's energy level appeared to drop as the match wore on.
"I saw that maybe he had some physical issues ... it's part of our sport," Khachanov said after advancing 6-7 (5-7) 6-3 6-2 6-2.
"What I can say from my side is I tried to give my best performance, fighting, fighting. I'm super happy to get this win."
The result meant Khachanov has now reached the third round of all four grand slams in a single calendar year for the first time.
Khachanov, who made a run to the US Open semis in 2022, broke Auger-Aliassime's serve seven times and saved seven of the nine break points he faced en route to only his second top-10 win at a grand slam.
Up next for the Russian will be Benjamin Bonzi, who eliminated Peruvian Ignacio Buse 6-2 7-6 (7-4) 6-0.
Gael Monfils bid adieu to grand slam tennis with a hard-fought loss to Learner Tien.
The 40-year-old Frenchman, who is planning to retire at the end of the year, was knocked out 6-3 6-4 6-3 by a player half his age.
Ninth seed Taylor Fritz brushed aside Mattia Bellucci 6-0 6-1 6-1 to extend his emphatic start to the tournament without dropping a set.
Fritz is among a handful of contenders looking to end a 23-year American wait for a men's grand slam singles champion after Andy Roddick's Flushing Meadows triumph.