After just 25 minutes, Djokovic was 5-0 ahead and on cruise control, Bautista Agut severely struggling to keep the Serbian at bay.
A short rain delay late in the second set allowed Bautista Agut to regroup, however.
He returned a completely different player and at the end of an absorbing two hours and 29 minutes, it was the number 22 seed who will now meet defending champion John Isner for a place in the semi-finals.
“Of course this is very special for me,” said Bautista Agut. “I just tried to play aggressive.”
Djokovic left court and headed straight to a press conference to try and explain this most remarkable of turnarounds at Hard Rock Stadium.
“He is a solid player but I shouldn’t have lost this match, I had way too many wasted opportunities,” said the world number one, who also failed to make the latter stages of Indian Wells this month.
“I thought I played well today and during this tournament but two or three sluggish games and that’s what happens.”
Djokovic, who also hinted his vociferous involvement in ATP tennis politics which saw President Chris Kermode ousted this month may have affected his focus, was initially consistent with his serve during the opening set.
The 31-year-old won 68 per cent of points on his first serve and 63 per cent on his second and mixed up his shots superbly, blending in power and subtlety to keep his opponent guessing.
A combination of his level dropping and Bautista Agut rising to the challenge saw a complete reversal in fortunes in the second set.
Bautista Agut was broken instantly yet the Spaniard responded well, especially after a 30 minute delay which saw him fight back and grab two set points, the second of which was taken when the reigning Wimbledon, US Open and Australian Open champion netted a forehand.
He broke the Serb again for 3-1 in the third following an uncharacteristic double fault and although Djokovic never shirks a battle and hit back in the very next game, it was the Spaniard who took advantage of his opponent’s faltering serve to move within two games of victory.
Serving for the match, he brought up two match points, another forehand winner sealing one of the best comebacks of his career.
Big-serving Isner moved into the last eight with a controversial 7-6 (7/5), 7-6 (7/3) triumph over Great Britain’s world number 22 Kyle Edmund.
In a razor-sharp encounter, Edmund was furious at 3-5 in the second set tie-break and was involved in a lengthy argument with chair umpire Carlos Bernardes after the 24 year-old stopped during the point.
Edmund was reacting to something said in the crowd but refused to divulge any further information.
“I obviously heard something but I’m not going to get into it,” he snapped to reporters afterwards.
Elsewhere there was a brilliant win for 18 year-old Felix Auger-Aliassime, the Canadian who came through qualifying, who beat Georgia’s Nikoloz Basilashvili 7-6 (7-4), 6-4.
Auger-Aliassime ,who is set to become the first player born in the 2000s to break into the top 50 when the rankings are updated next week, plays Borna Coric after the Croatian beat Nick Kyrgios 4-6, 6-3, 6-2.
The controversial Australian was furious after being heckled by a fan during the third set and was docked a point after responding with an expletive.
“I’m playing for two hours and 20 minutes, and a guy yells at me, ‘Play some tennis,’ I’m not going to take it.
“So I said F you to him,” said Kyrgios, who admitted he became “a little bored” during the second set.”
In the women’s draw, Estonia’s Anett Kontaveit reached the semi-finals in Miami for the first time after she brought an end to Hsieh Su-Wei’s impressive run with a 3-6, 6-2, 7-5 victory.