Dimitrov’s chance appeared to have passed when he failed to take advantage of a first match point in the second set breaker and started to succumb to cramp.
But the Bulgarian somehow broke at the beginning of the final set before holding on to seal the biggest win of his career in three hours and four minutes.
The first set appeared to be going according to plan for Djokovic in the early stages as he held mostly without any drama and quickly slammed the door shut on the one occasion where Dimitrov did manage to earn himself some break points.
The world number one even held a set point in the 11th game of the set but Dimitrov saved it with a backhand volley winner that belied the confidence of a man currently enjoying his career high ranking of 28.
In fact it was Dimitrov who broke first, the Bulgarian seizing the advantage in the very next game only to be pegged back by Djokovic, the Serb forcing the breaker.
Time and again throughout the match Dimitrov pegged Djokovic back, levelling up from a 4-2 mini-break down early in the first tie-break and then saving more set points before going on to snatch the first set from underneath Djokovic.
It all appeared to be spinning rapidly out of control for Djokovic when he found himself a break down early in the second set, only for that to be compounded by two missed break back opportunities and a recurrence of a recent injury, when he rolled his right ankle on the baseline.
An immediate injury timeout followed before Djokovic got things back on level terms by breaking back, failing to secure the one remaining break point he held before the delay but fashioning himself another chance just moments later.
But again Dimitrov was not to be undone, the Bulgarian holding firm to force a second breaker before denying Djokovic on a number of set points.
The drama was set to increase exponentially as Dimitrov succumbed to cramp, stopping mid-rally on a Djokovic set point.
Djokovic somehow contrived to miss what should have been an easy forehand winner to level the match at one set apiece and Dimitrov took the opportunity to earn himself a match point with a second serve ace.
The 21-year-old was still battling with cramp, however, and could not take advantage as Djokovic finally levelled up at the fourth attempt.
It looked for all the world that Dimitrov would have nothing left for the third set, after conceding the second with a weary forehand into the net, but from nowhere he fashioned a break in the opening game of the decider.
From there he simply held on, denying Djokovic the chance to break back on two separate occasions with gutsy play from the baseline.
Whether the Bulgarian would have the mental fortitude to serve out the biggest win of his career became a moot point when Djokovic handed Dimitrov two more match points with a forehand long of the baseline.
Only the one was required, however, Djokovic pushing a change-of-direction forehand wide down the line to hand Dimitrov a place in the third round, where he will face either qualifier Santiago Giraldo or 15th seed Stanislas Wawrinka on Thursday.
Second seed Roger Federer enjoyed a much easier time of it as he began the defence of his Madrid Masters title with a comfortable 6-3 6-3 win over Radek Stepanek.
Having triumphed on the controversial blue clay last year, Federer looked no less at ease with the return to the traditional red dirt, the Swiss world number two racing out to a 6-3 5-3 lead before suffering his only blip in concentration.
Federer, who had not played a match in almost two months (since the Indian Wells quarter-finals in March), showed only the faintest glimpse of rustiness as he failed to serve out the match instead handing Stepanek a break back.
But the 17-times Grand Slam champion quickly made amends breaking again in the ninth game of the second set to book his place in the third round after just 82 minutes.
"I didn't think I played incredible, but that's not what I was expecting myself to do here, but I didn't play bad either," Federer, who was also sporting mint-green flashes on his tennis shoes.
"Overall, I'm very happy, because he (Stepanek) has caused me difficulties in the past," added the 31-year-old world number two, a three-times champion in Madrid.
"Today that wasn't the case and I thought I was pretty much in control."
Federer is yet to win a tournament this year but if he defends his Madrid crown he will equal John McEnroe's 77 career titles and join the American in third on the all-time ranking behind Jimmy Connors on 109 and Ivan Lendl with 94.
"Obviously I've had a very successful career, which has been amazing already. More than I ever thought I would achieve," Federer said.
"I would love to tie McEnroe at 77, no doubt about it. He was an amazing player and brought a lot to the game, like some other great champions and legends have done and paved the way for us."
Next up for Federer will be either 14th seed Kei Nishikori or Serbia’s Victor Troicki.
Second round results
Grigor Dimitrov (Bulgaria) beat 1-Novak Djokovic (Serbia) 7-6(6) 6-7(8) 6-3
3-Andy Murray (Britain) beat Florian Mayer (Germany) 7-6(11) 7-6(3)
2-Roger Federer (Switzerland) beat Radek Stepanek (Czech Republic) 6-3 6-3
Fernando Verdasco (Spain) beat 12-Milos Raonic (Canada) 6-4 2-6 7-6(7)
16-Gilles Simon (France) beat Jeremy Chardy (France) 6-4 7-6(5)
Daniel Gimeno-Traver (Spain) beat 8-Richard Gasquet (France) 7-5 3-6 6-4
Pablo Andujar (Spain) beat John Isner (US) 6-4 6-4
First round results
Tommy Robredo (Spain) beat Marcos Baghdatis (Cyprus) 6-4 6-2
Viktor Troicki (Serbia) beat Marcel Granollers (Spain) 7-5 4-6 6-2
13-Tommy Haas (Germany) beat Andreas Seppi (Italy) 6-1 6-2
15-Stanislas Wawrinka (Switzerland) beat Marius Copil (Romania) 6-4 6-4
Mikhail Youzhny (Russia) beat Fabio Fognini (Italy) 7-6(4) 2-6 7-6(5)
Juan Monaco (Argentina) beat 9-Janko Tipsarevic (Serbia) 7-6(5) 6-3
Santiago Giraldo (Colombia) beat Martin Klizan (Slovakia) 6-2 6-4
Benoit Paire (France) beat Joao Souza (Brazil) 6-1 7-6(0)