For the first 50 minutes of the contest, Wawrinka played as if possessed by his compatriot Roger Federer and confounded the world No.1 with brilliant backhands and ferocious forehands to notch up five straight breaks of serve.
Even when Djokovic had weathered the worst of the storm and clawed his way back from 6-1, 4-1 down to win the second and third sets, the world No.17 made the Serbian earn every point and even forced a decider after a dramatic tie-break.
The Swiss blew four precious break points in the ninth game of the fifth set and Djokovic converted his third match point with a brilliant backhand winner to seal a 1-6, 7-5, 6-4, 6-7, 12-10 win.
In a conscious echo of his win over Rafa Nadal in last year's nearly six-hour final on Rod Laver Arena, Djokovic ripped off his shirt and roared in delight at a victory which kept alive his hopes of a third successive title at Melbourne Park.
"We are only halfway through the tournament, but it felt like a final to me," said Djokovic, who will meet fifth seed Tomas Berdych in the next round.
"He deserved equally to be a winner of this match. Last night he showed his qualities. He was the aggressor on the court and I was just hanging in there trying to fight.
"I always believed I could win the match and I am absolutely thrilled to be in the next round. It certainly brings back memories of last year with Rafa."
Wawrinka, who left the court with tears in his eyes, was distraught after coming up short despite playing the match of his life.
"I think it's by far the best match I ever played, especially in five sets against the No.1 player," he said.
"At the end I was really, really close, so for sure I'm sad. But I think there is more that is positive than negative.
"At the end he was still there. He was playing great tennis. We were both tired, but I really fought like a dog.
"For sure I was serving for the second set to be up two sets to love, but in five sets, five hours, you always have some opportunity to win a set or to win the match," Wawrinka added. "If you don't take it, he's going to take it."
Despite the defeat, Wawrinka believed the way he had played against Djokovic could spur him on to bigger and better things.
"I think I can use it, I hope I will use it," he said. "I think it's important if I want to keep improving myself, my game, and to try to come back closer to the top 10."