The world number one went into the contest with the 19-year-old having not dropped a set at these championships but found himself in big trouble after a gruelling first couple of hours.
It felt like a different match entirely after that, as Djokovic won 16 of the final 17 games before Musetti retired with the scores at 6-7 (7-9) 6-7 (2-7) 6-1 6-0 4-0 in the 2016 champion's favour.
The Serbian seemed unsettled by Musetti's unpredictable early approach, the teenager mixing up forehand speeds and backhand passes to good effect after an early exchange of breaks.
It looked like Djokovic had control of the opening tie-break only for Musetti to win five out of six points to lead 6-5. Two rasping forehands soon secured the set after a Djokovic error.
Belief in a shock upset really did begin to grow when Musetti took a 3-1 lead in the second set, at which point Djokovic literally took his hat off to his opponent. Whether it was psychological or his cap really was a bother, a bare-headed Djokovic promptly broke back to love.
Djokovic's error count dropped from 20 in the first set to 15 in the second, but the momentum still seemed to be with the Italian, who continued to paint the lines from both sides of the court even when it seemed impossible: early in the second tie-break, a reflex lob from the net somehow bounced on the baseline as his opponent watched in disbelief.
Deserved as his lead was, there was still a feeling that, should Musetti's standards slip even a touch, the door to the comeback would be open. Djokovic seemed to sense as much, returning from a bathroom break to power his way through the third set in just 28 minutes, less than half the time of each of the first two.
Suddenly, doubt crept into Musetti's play as Djokovic began to dictate. He won 16 points in a row to take a 4-0 lead in the fourth and broke again with the sort of drop-shot winner that Musetti had anticipated with ease in the opening two hours.
Djokovic was troubled by his lower back before the fifth set and needed treatment to his hand after somehow winning the first point on the Musetti serve despite falling heavily in the dirt.
Yet it was Musetti whose body could simply no longer keep up, his retirement ensuring Djokovic will now face Matteo Berrettini in the last eight.