Two-time defending champion Novak Djokovic avoided being the latest seed to lose at the upset-hit Shanghai Masters, rallying to defeat No. 85-ranked Mikhail Kukushkin 6-3, 4-6, 6-4 in the third round Thursday.

Roger Federer also advanced to the quarterfinals with a routine 6-4, 6-2 win over Roberto Bautista Agut, a night after he saved five match points and escaped with a three-set win in the second round.

Djokovic extended his 27-match winning streak in China, but it wasn't easy against the speedy Kukushkin, who kept the top-ranked Serb running with powerful forehands that repeatedly caught the lines and impressive defense to extend lengthy rallies.

Serving at 4-5 in the second set, a frustrated Djokovic even lashed out at the chair umpire after receiving a time violation as he caught his breath between serves.

"Did you not see the point before that we played?" he asked. "You're sitting there enjoying your time, right?"

After dropping that service game to lose the set, the seven-time major winner got the early break in the third and held on for the win. It was the first set he's lost in seven matches this year in China, including five at last week's China Open.

"He disguised his first serve very well. He disguised his forehands very well. Definitely was the toughest match I played so far in China," Djokovic said. "When you play somebody who has nothing to lose, it's important that I got the W."

Kukushkin, a Russian-born Kazakh player who entered the match with a 1-15 record against top-10 players, had 35 winners to 27 for Djokovic.

Federer avoided the dramatics of his opening match over Leonardo Mayer of Argentina, which he finally won 7-5, 3-6, 7-6 (7) after coming back from a 5-2 deficit in the third-set tiebreaker.

Federer, who will overtake Rafael Nadal at No. 2 in the rankings next week, played a much cleaner match against Bautista Agut, hitting 33 winners to just 21 unforced errors. He had 57 unforced errors against Mayer.

The biggest problem after Wednesday's match was the recovery. The match ended at 12:30 a.m. local time and he didn't get to sleep until four hours later.

"I think that's the toughest part. Just maintaining a good concentration, not getting frustrated, taking chances, being inspired on the court. All those things can chip away once you go to bed so late," he said.

Djokovic will next face David Ferrer, who boosted his chances of qualifying for the ATP finals with a 2-6, 6-1, 6-2 win over Andy Murray.

Both Murray and Ferrer are fighting to claim one of the remaining spots in the eight-man tournament next month in London. With the win, Ferrer will now pass Murray into ninth place in the standings, just behind Milos Raonic in eighth.

Murray had played far more tennis than the fresher Ferrer in recent weeks, winning a title in Shenzhen and making the semifinals of the China Open. The Spaniard, meanwhile, was coming off two straight opening-round losses.

"He literally barely missed a ball for about a set and a half," Murray said. "I couldn't find a way around him."

Murray will now try to build on his momentum over the rest of the season to make a push for London. Besides last year when he was recovering from back surgery, Murray has played in the season-ending championships every year since 2008.

"There's still obviously a few more weeks left in the season," Murray said. "I've played some pretty good tennis."

Sixth-seeded Tomas Berdych also reached the quarterfinals, beating Ivo Karlovic 6-3, 6-4.

The Czech player is the only seed remaining on the bottom half of the draw after Nadal, Stan Wawrinka, Marin Cilic, Milos Raonic and Ernests Gulbis were all knocked out. No. 13-seeded John Isner also lost Thursday to Spaniard Feliciano Lopez, 6-3, 6-4.

Berdych will next face Gilles Simon of France in the quarterfinals, while Lopez takes on Russian Mikhail Youzhny.

Federer plays French veteran Julien Benneteau, who defeated Jack Sock of the United States, 6-3, 6-4.