The 15th-seeded Swiss had taken an early 6-1 5-2 lead over the world number one with a powerful serve and booming groundstrokes before Djokovic took greater control of the rallies and waited for Wawrinka to make mistakes.
Wawrinka, however, managed to send the match into a decider when he totally dominated the fourth set tiebreak before the world number one clinched a 1-6 7-5 6-4 6-7 12-10 victory on his third match point with a cross-court backhand winner.
The match, which lasted five hours and two minutes, finished at 0140 (1440 GMT).
"It's really hard to find the words to describe the feeling," an exhausted Djokovic said courtside.
"He deserved equally to be a winner of this match. I give him a lot of credit. He showed his qualities.
"He was the aggressive player on the court. He was playing well, mixing it up, using the right tactics and serving really well.
"I was just trying to hang in there and fight and give every drop of energy."
Djokovic, seeking to become the first man in the professional era to win three successive Australian Open titles, will now meet fifth seed Tomas Berdych in the quarter-finals.
Wawrinka had lost his 10 previous encounters against Djokovic but did not appear overawed by the occasion or the world number one.
He mixed up the pace of his returns, painted the tramlines and launched ground shots from both wings that had the crowd in Rod Laver Arena ooh-ing, ah-ing and looking at neighbours wondering if what they were watching was real.
He broke Djokovic three times to claim the first set 6-1 in 25 minutes and, while he had his serve broken in the first game of the second set, the setback was temporary as he then reeled off the next four games.
Djokovic, who had changed his shoes after the third game of the second set because he was constantly slipping over, however finally held serve at 4-2, which sparked a recovery.
The world number one won five of the next six games to take the set 7-5 and level the match, then broke in the first game of the third that produced an almighty roar from the Serb.
While Wawrinka broke back immediately, both players then seemed to take a breath and the match settled down with Djokovic taking greater control of the rallies, reducing Wawrinka's ability to go for winners and waiting for him to make errors.
The tactic worked as he broke in the ninth game of the third set then held serve to take it 6-4 before the match quickly advanced through the fourth as both held serve until the tiebreak, which Wawrinka dominated.
Neither were able to hold their first service games in the fifth set and it then became a case of who would blink first with Djokovic holding a break point in the eighth game only for Wawrinka to hold his nerve.
The match then stayed on serve until the 22nd game of the set, where Djokovic held three match points before he finaqlly sealed the win, raised his arms and let out another almighty bellow of relief.
"It could have gone either way this match and I am thrilled to be through," Djokovic added.
"He had a lot of chances to win this match. All the credit to him, I feel sorry that one of us had to lose."
(For more sports news and updates, follow Ahram Online Sports on Twitter at @AO Sports and on Facebook at AhramOnlineSports)