The 39-year-old Swiss prevailed 7-6 (7-5) 6-7 (3-7) 7-6 (7-4) 7-5 over Dominik Koepfer on Saturday in the longest match he has played in 18 months, following double knee surgery. The contest lasted three hours and 35 minutes.
Federer laboured at times, making 63 unforced errors, fighting hard to triumph in front of an empty crowd locked out by Paris' 9pm curfew, with the match finishing at close to 1am.
The 20-time grand slam winner has played few tournaments over the past 18 months and said a need to rest his body was behind his decision to withdraw.
In a statement released by the French Open, Federer said: "After discussions with my team, I've decided I will need to pull out of Roland Garros today.
"After two knee surgeries and over a year of rehabilitation it's important that I listen to my body and make sure I don't push myself too quickly on my road to recovery.
"I am thrilled to have gotten three matches under my belt. There is no greater feeling than being back on court."