The 28th-ranked Bulgarian saved three set points in the first before taking the lead, and Djokovic then appeared to hurt his right ankle when trailing 4-2 in the second. The Serbian player slipped on the baseline and winced in pain, then immediately called for a trainer and took a lengthy break to get treatment.
The wait for the game to resume annoyed the crowd, who turned against Djokovic and began chanting Dimitrov's name.
"I seriously didn't expect that," said the 21-year-old Bulgarian. "That was the one thing that of course cheered me up a lot."
Djokovic -- who ended Rafael Nadal's eight-year winning streak at the Monte Carlo Masters last month -- said the loss had more to do with poor preparation than an injury.
"I didn't prepare myself so good," Djokovic said. "For 12 days after Monte Carlo I haven't touched the racket."
Djokovic said he didn't understand why the crowd turned against him during the injury break.
"I don't see any reason for that. I didn't do anything bad," he said.
The crowd then displayed sympathy for Dimitrov when he appeared to suffer from cramps at 5-5 with the game at deuce, visibly upsetting the Serb. Dimitrov, who suffered severe cramp last year at Roland Garros, said it didn't affect him as badly this time.
"Today I was better, I was not crawling."
The second-ranked Federer took a break after losing in quarterfinals of the Indian Wells hardcourt tournament to Nadal, but instantly took to the slower red surface and broke early to take control of the first set. He then broke Stepanek three times in the second, losing his own serve once, to finish the win in 1 hour, 21 minutes.
"I'm very happy, because he has caused me difficulties in the past," Federer said about the 34-year-old Stepanek, who these days is one of the few players on tour older than the Swiss star.
"If I would be the only guy over 30, it would feel a bit awkward, to be honest," the 31-year-old Federer said.
Federer can equal John McEnroe's career total of 77 titles if he defends the Madrid trophy, having won last year on the heavily criticized blue clay that organizers decided to get rid of for this tournament.
Like many of his fellow players, Federer said the traditional red surface was better.
"They (organizers) spoke to more experts from the French Open and Monaco, and you can tell it's a proper clay court now," Federer said, adding that Madrid had issues with its courts even before switching to a blue surface. "I know that added to the whole controversy, the color, because of tradition and so forth. It was extremely slippery and the court wasn't taking the water well."
Murray said he struggled with the altitude and found it hard to breathe at the end of the first set where "there were so many long points."
Madrid is 2,180 feet above sea level and the Scotsman said Mayer's array of shots also made it difficult by constantly varying the pace.
"It's hard to explain, but if you're playing someone that plays with a nice rhythm it's a lot easier to breathe during the points," Murray said.
Thirteenth-ranked Tommy Haas of Germany and No. 15 Stanislas Wawrinka of Switzerland were among other seeded players to advance, while No. 8 Richard Gasquet of France lost 7-5, 3-6, 6-4 to Daniel Gimeno-Traver of Spain.
The top-ranked Williams struggled a bit to counter her opponent's drop-shots and lobs but stayed on course for a fourth title of the year and the 50th of her career.
"It was a really good clay-court tennis match for me," Williams said. "I know if I face another opponent like that I'll have that experience and I'll know how to play."
"When you start so late it's important to make sure you're ready and awake and focused from the beginning of the match," Sharapova said. "I did a good job in that today."
"My ankle was hurting me at the beginning of third set, but I was really able to focus and went throughout pain," Bartoli said.