MADRID — Facing match point and on a second serve, Roger Federer decided it was time for a "panic move."

So he went into all-out attacking mode, charging to the net to try to avoid a disappointing end to his first clay-court tournament since 2016.

It worked.

A lucky volley, followed by a smash winner, kept Federer in the match. An aggressive forehand two points later allowed him to save a second match point, and he went on to beat Gael Monfils 6-0, 4-6, 7-6 (3) in the third round of the Madrid Open on Thursday.

"I felt my chances were bigger at the net than from the baseline, so it was a desperation, panic move," Federer said. "I was very nervous, so I just figured, 'come to the net, take a chance,' and it worked out."

In only his second match on clay in three years, Federer rallied from three games down late in the third set and then dominated the deciding tiebreaker to outlast Monfils and reach the quarterfinals in a match of more than two hours at the Magic Box centre court.

It was the 1,200th career win for Federer, allowing him to join Jimmy Connors as the only male players to have reached the milestone in the Open Era.

"I'm not sure if I've lost a match before after winning the first set 6-0. The statistics show that usually you are in good shape. But with Gael you really never know," Federer said. "He really got into a great groove after that, he had great intensity. At the end I played a great tiebreaker so I'm very relieved and I'm very happy right now."

Top-ranked Novak Djokovic, second-seeded Rafael Nadal and defending champion Alexander Zverev also advanced to the last eight on Thursday.

In the women's quarterfinals, top-ranked Naomi Osaka lost 3-6, 6-2, 7-5 to Belinda Bencic, falling to the Swiss for the second time this season.

Bencic will now face two-time Madrid champion Simona Halep, the former No. 1 who defeated Ashleigh Barty 7-5, 7-5.

American Sloane Stephens got past Petra Martic in two sets and will face Kiki Bertens, who upset defending champion Petra Kvitova 6-2, 6-3 in a rematch of last year's final.

The fourth-seeded Federer had eased past Richard Gasquet in his first match in Madrid and looked set for another comfortable win after routing Monfils in less than 20 minutes in the opening set. But the Swiss lost five straight games to fall behind the 18th-ranked Frenchman who was seeking his second title of the season.

Federer, who looked frustrated at times, recovered after being down 0-3 and 1-4 in the third set. He saved the two match points in a long game when trailing 5-6. The 20-time Grand Slam champion was back in control in the tiebreaker, winning six of the first seven points.

Monfils, who won only nine points in the first set, was seeking his 19th win this season. He won the Rotterdam title in February.

Federer will next face fifth-seeded Dominic Thiem, who defeated Fabio Fognini 6-4, 7-5. The fifth-ranked Austrian is coming off titles in Barcelona and Indian Wells. He was runner-up in Madrid the last two years.

Federer, who earned his 20th win of the season, skipped the clay swing the last two years to remain fit for the rest of the season. The Madrid Open is his first clay tournament as he prepares to return to the French Open after a three-year absence. He has already won two hard-court titles this season, in Dubai and Miami, and is seeking his third Madrid trophy, and first since 2012.

Nadal stayed in contention for his sixth Madrid title with a 6-3, 6-4 win over American Frances Tiafoe.

"I played with better rhythm and showed some positive things," said Nadal, who missed practice time earlier in the week because of a stomach virus. "I'm satisfied."

Earlier, top-ranked Novak Djokovic reached the final eight by beating Jeremy Chardy 6-1, 7-6 (2). It was his 13th consecutive win over Chardy, all in straight sets.

"You do have a little bit more of a confidence coming into a match knowing that you've never lost to your opponent," Djokovic said. "But he was playing well here."

Djokovic will next face Marin Cilic, who came from behind to beat Laslo Djere 4-6, 6-3, 6-2.

Also advancing to the quarterfinals were Stefanos Tsitsipas and Stan Wawrinka, who defeated sixth-seeded Kei Nishikori in two sets. Wawrinka will be Nadal's opponent, while Tsitsipas will face Zverev, a three-set winner over Hubert Hurkacz.

___

Tales Azzoni on Twitter: http://twitter.com/tazzoni

___

More AP tennis coverage: https://apnews.com/apf-Tennis and https://twitter.com/AP_Sports