MADRID -- Grigor Dimitrov stunned Novak Djokovic in the second round of the Madrid Open on Tuesday, beating the top-ranked Serb 7-6 (6), 6-7 (8), 6-3 for the biggest win of his career.

The 28th-ranked Bulgarian saved three set points in the first before taking the lead, and Djokovic then appeared to hurt his right ankle while 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 match to resume annoyed the crowd, who turned against Djokovic and began chanting Dimitrov's name.

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 racquet."

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.

Earlier, defending champion Roger Federer looked comfortable on clay in his first match in two months, easing past Radek Stepanek of the Czech Republic 6-3, 6-3 to reach the third round.

In the women's half of the draw, defending champion Serena Williams moved into the third round after beating Lourdes Dominguez Lino of Spain 6-2, 7-5.

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."

Third-seeded Andy Murray had a tougher time against Florian Mayer before outlasting the German 7-6 (11), 7-6 (3). Murray said he struggled with the altitude and found it hard to breathe at the end of the first set when "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 her fourth title of the year and 50th in 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."

Also, Marion Bartoli of France overcame a fall to beat qualifier Maria-Teresa Torro-Flor of Spain 6-4, 2-6, 6-4.

"My ankle was hurting me at the beginning of third set, but I was really able to focus and went throughout pain," Bartoli said.