World number 76 Musetti's stylish game was threatening to send Djokovic to his earliest Roland Garros exit since 2009.
World number one Novak Djokovic survived a huge scare as he fought back from two sets down against teenager Lorenzo Musetti to reach the French Open quarter-finals on Monday after his Italian opponent retired with cramps in the fifth set.
Top seed Djokovic was outplayed by an inspired Musetti as he lost two tiebreaks, but the match then changed dramatically as the Serb fought back to 6-7 (7), 6-7 (2), 6-1, 6-0, 4-0 before progressing to the last eight in Paris for the 15th time.
He will play another Italian after Matteo Berrettini was handed a walkover after Roger Federer's withdrawal.
World number 76 Musetti's stylish game was threatening to send Djokovic to his earliest Roland Garros exit since 2009.
But having produced tennis of the highest calibre in two sensational sets, Musetti blew up as the effort he had expended to eclipse Djokovic caught up with him.
The 19-year-old Musetti lost the third set in 24 minutes and did not even get a point in set four until the fifth game as the match slipped away from him.
"It's not an injury," he explained later.
"It's just a little bit of cramps and a little bit of low back pain.
"There was no chance that I could win a point, so I decided to retire because I think it was the best thing."
A relieved Djokovic went 4-0 ahead in the decider before Musetti quit in an anti-climactic end to what was brewing up to be a seismic shock in a tournament already full of surprises.
Djokovic said he had felt nervous before the match and even "liked" the fact that he lost the opening two sets.
"I don't know, I just played under a certain kind of tension and wasn't able to go through my shots," he told reporters.
"Just not playing and not feeling great in the first couple of sets. But credit to him for playing well in important moments. After I lost the second set and went out to change and came back, I just felt different. I was a different player."
Musetti dropped serve early on but hit back when Djokovic made a backhand error and from then on went toe-to-toe with the 18-time Grand Slam champion who appeared unsure how to counter the all-court flair of his young opponent.
Djokovic was 4-1 ahead in the first set tiebreak but Musetti unleashed two stunning single-handed backhand winners to close the gap and then carved out a set point when Djokovic dragged a forehand into the net at 5-5.
Musetti smiled ruefully when Djokovic saved that with a forehand flush onto the sideline. But the young Italian swiped away two forehand winners to take the opener.
A rattled Djokovic blazed a forehand wide to surrender serve early in the second set but quickly replied as Musetti's concentration wavered.
Djokovic had to dig deep to save break points at 2-3 and 3-4 and eventually took the set into a tiebreak.
A terrible misjudgement by Djokovic saw him leave an improvised lob from Musetti that landed on the baseline with the Serb bizarrely offering no shot.
When Musetti punished more Djokovic errors to move two sets ahead it seemed he was on the brink of a momentous victory.
Djokovic left the court to change clothes and when he returned, he might have suspected Musetti had been replaced by a look-alike. He broke serve straight away and suddenly the Italian looked lost, making basic errors and appearing sluggish.
Djokovic needed no second invitation to take advantage and lost only 14 points in levelling the match in a flash -- the third and fourth sets lasted 46 minutes in total compared with the 75 minutes it took Musetti to win the superb opener.
After that, there was no way back for teenager Musetti as the 34-year-old Djokovic overturned a two-set deficit for only the fifth time in his career.
"It's unfortunate for a young player like him," Djokovic said.
"He was unable to physically sustain the level."
French Open PIX: Djokovic survives; Gauff in quarters
Murray backs Federer's 'sensible decision'
Rock music helps Swiatek roll on at French Open
Serena stunned by Rybakina in French Open fourth round
Federer withdraws from French Open