The second-seeded Serb won 6-3, 6-2, 6-7 (2), 6-4 against Marin Cilic, improving to 9-0 in their head-to-head meetings, but looked sluggish at times.
Federer, meanwhile, was in total control before wasting four set points in the second set on his way to a 7-5, 6-7 (7), 6-2, 6-4 win against Dmitry Tursunov.
"I'm pleased I found a way and played it solid and tough toward the very end," said Federer, who advanced to the fourth round at Roland Garros for the 10th straight year. "When somebody serves big like he does, it's sometimes a bit difficult."
Djokovic dropped serve early to trail 3-1, made sloppy unforced errors in the tiebreaker, and was broken back in the fourth set after leading 4-2. He sealed the victory on his first match point when the 25th-seeded Croat double-faulted.
"Physically I had to work very hard, because he was very aggressive," Djokovic said. "First two sets and beginning of the third I had some chances to break him and get the job done in straight sets, but he started playing a little bit better."
Sixth-seeded Tomas Berdych of the Czech Republic downed Spaniard Roberto Bautista Agut 6-1, 6-2, 6-7 (4), 6-4. He next faces 10th-seeded American John Isner, who beat No. 17 Tommy Robredo of Spain 7-6 (13), 7-6 (3), 6-7 (5 ), 7-5.
Isner became the first American man to reach the French Open's fourth round since 2010, and the first man from the U.S. to get to the final 16 at any Grand Slam tournament since Andy Roddick and Mardy Fish at the 2012 US Open, although Fish withdrew before his fourth-round match.
Djokovic is looking to win the French Open for the first time. He lost to eight-time champion Rafael Nadal in the semifinals last year.
In the tiebreaker, Djokovic's weak drop shot at 6-2 down gave Cilic an easy chance to whip a forehand winner.
Frustration got to Djokovic in the eighth game of the fourth set, with the player shaking his head when another Cilic shot clipped the net and bounced in to give him a break point.
Djokovic vigorously pumped his fist after saving it but then gave another chance with a backhand into the net. He got out of trouble with a strong serve that Cilic returned into the net, but Cilic took his next chance, breaking back to 4-4 with a backhand volley at full stretch at the net.
Federer, who had won his opening two matches in straight sets, converted only four of his 21 break-point opportunities against the 31st-seeded Russian.
The 17-time Grand Slam champion even joked when asked, moments after the match, whether his poor conversion rate worried him.
"I pretend there isn't a problem," he said, breaking into a huge grin. "I'll go for the next one, and the next one, and the next one."
He's done far worse recently. In his quarterfinal win against Tsonga at this year's Monte Carlo Masters in April, Federer wasted 15 straight break points.
Tursunov was trailing 0-40 in the 12th game and 7-6 down in the tiebreaker but went on to level the match with a forehand pass down the line.
"I wish at times I could have played a bit more offensive," Federer said.
Shortly after winning the second-set tiebreaker, Tursunov appeared to have a minor meltdown while trailing 2-1 in the third. He argued with the chair umpire and took a medical timeout for what he later said was discomfort in his hip. He left the court for several minutes.
"Overall, the first two sets ... I felt that it was a fairly even battle out there," said Tursunov, who was broken only once in the first two sets. "It's hard for me to predict, but definitely playing on one leg is not going to make things easier. Tough luck for me, but hopefully I'll get him somewhere else."
Federer is trying to win the title at Roland Garros for the second time. He is the 2009 champion, but lost in the quarterfinals last year. The Swiss star next plays No. 18 Ernests Gulbis of Latvia, who reached the fourth round here for the first time since 2008 -- the year he reached the last eight -- after beating Radek Stepanek 6-3, 6-2, 7-5.
"I don't want to tell all my cards. But I have a game plan," Gulbis said. "You don't need to be scared to do certain things against him."
Federer leads Gulbis 2-1. All of those matches were in 2010, and they are 1-1 on clay.
"I'm aware of the challenge ahead of me," Federer said. "I have had some tough matches with him in the past."
The Associated Press contributed to this report.