The Spaniard, who won the title in 2009, came through 4-6 6-3 6-7 (5-7) 6-3 6-2 in four hours and six minutes.
Zverev, 19, had recovered a break early in the final set before requiring treatment for cramp.
Nadal, 30, goes on to face France's Gael Monfils, who beat German Philipp Kohlschreiber 6-3 7-6 (7-1) 6-4.
Canadian third seed Milos Raonic made it through to the last 16 with a 6-2 7-6 (7-5) 3-6 6-3 win over Frenchman Gilles Simon.
Zverev has long been touted as a future world number one and it appeared as though he would make his Grand Slam breakthrough against Nadal.
The teenager's big serve and brilliant backhand earned him a 2-1 lead after three sets, only for ninth seed Nadal to battle his way back in characteristic fashion.
It is almost three years since Nadal won his 14th major title at the 2014 French Open, and that was the last time he got past the quarter-finals at a Grand Slam.
Injuries have taken their toll, but it was Zverev whose fitness failed him in the closing stages on Rod Laver Arena.
The German won a gruelling 37-shot early in the final set but the damage was done as he could not recover fully, despite treatment from the trainer.
"I won by fighting and running a lot," said Nadal. "I think everybody knows how good Alexander is. He's the future of our sport and the present too.
"It's been a very tough match for me. I didn't start playing my best and I was not feeling very well because I was losing too much court. When I felt I was feeling better I had more time to control from the baseline.
"It was a close one but he deserved to play a little more aggressive than me. I had to fight for every point."
Denis Istomin, the qualifier from Uzbekistan who stunned Novak Djokovic in round two, produced another superb effort to beat Spanish 30th seed Pablo Carreno Busta 6-4 4-6 6-4 4-6 6-2.
He will next play the winner of Saturday's late match between Grigor Dimitrov and Richard Gasquet.
Belgian 11th seed David Goffin impressed with a 6-3 6-2 6-4 win over Croatia's Ivo Karlovic, setting up a clash with Austrian eighth seed Dominic Thiem, who beat Frenchman Benoit Paire 6-1 4-6 6-4 6-4.
Thirteenth seed Roberto Bautista won the all-Spanish battle with 21st seed David Ferrer 7-5 6-7 (6-8) 7-6 (7-3) 6-4 and next faces Raonic.
Former champion Rafael Nadal overcame rising star Alexander Zverev in a gripping contest to reach the fourth round of the Australian Open.