Third-seed Federer took the first set 6-4 against France's Richard Gasquet but, in a match of clean-hitting strokes, lost the next two sets against the 16th seed on tiebreaks 7-6(2) 7-6(4).
"I should never have lost this match," said world number three Federer, knocked out by Ernests Gulbis in the second round in Rome last year.
"He knows how close he came to losing. I had multiple chances but I couldn't make the difference."
The Swiss, who had won the last eight matches between the two, broke serve in the very first game but Gasquet responded with 10 straight points to draw level at 2-2.
The long thrilling rallies had both players covering every bit of the court but it was Federer, playing with his usual grace, who broke again in the seventh game with two sweet forehands to take the set.
Gasquet, whose one victory against Federer had come on clay at Monte Carlo in 2005, fought back by trouncing the tired-looking Swiss in the second set tieback with a backhand winner before a flourishing crosscourt forehand.
With games going with serve in the deciding set, Gasquet again showed a steelier side, forcing Federer into two top-spin backhand errors in another confident tiebreak to take a scalp.
"I thought I was playing well but he served better towards the end and hung in there. I didn't feel I could break and it's no fun to play like that," said Federer.
Starting late in Rome evening, Djokovic began strangely out of sorts against 14th seed Wawrinka, but up 5-4 and 30-30, the Serbian punched a decisive crosscourt winner after an exhausting rally before producing an elegant dropshot to snatch the set.
After that the second set was a procession with Djokovic, who has won his last six tournaments, dispatching two passing shots to take a 5-0 lead before winning the set with an ace.
- Related links