"I am thrilled to have gotten three matches under my belt. There is no greater feeling that being back on court. See everyone soon!"
French Open tournament director Guy Forget said: "The Roland Garros tournament is sorry about the withdrawal of Roger Federer, who put up an incredible fight last night.
"We wish him all the best for the rest of the season."
This year was the first time Federer had contested the French Open since 2015.
The 21-time grand slam champion had played well in his first three outings at Roland Garros, defeating Denis Istomin, Dominik Koepfer and Marin Cilic.
He found himself in the unusual position of playing his third round match against Koepfer past midnight and in front of no crowd.
The local restrictions in France earlier this week had resulted in fans being kicked out of stadiums in the middle of matches in order for spectators to beat the 9pm curfew.
The decision to play night session matches — as part of a new TV broadcast deal — beginning from 9pm (local time) has been under fire all week, and the criticism may intensify with the news that Federer 39, may have played his final ever match at Roland Garros in front of an empty stadium pushing towards 1am.
Federer was due to face ninth seed Matteo Berrettini in the fourth round. The Italian will now progress automatically to the quarter-finals.
Federer is targeting a return to tennis for the grass court season and tilt at a ninth Wimbledon title.