Federer had won his last eight meetings against Gasquet but made a series of uncharacteristic errors from the baseline as the match wore on to let the Frenchman draw even and eventually take control.
"I definitely feel I should never have lost this match," Federer said. "I just couldn't come up with the shots."
Earlier, Rafael Nadal overcame a virus to beat fellow Spaniard Feliciano Lopez 6-4, 6-2. The start of the match was delayed by about 20 minutes as Nadal considered withdrawing.
"I had a fever last night and this morning I didn't feel better," Nadal said. "I didn't know if I was going to go on the court for the match. I decided at the last moment."
In his opening match on Wednesday, Nadal struggled past 148th-ranked qualifier Paolo Lorenzi in three sets.
"I didn't understand why I felt so slow yesterday, why I had no energy. I understand today," Nadal said.
To keep his No 1 ranking for at least one more week, Nadal will need to beat Marin Cilic of Croatia in the quarterfinals. Otherwise, Novak Djokovic could claim the top spot by winning the tournament.
Later, Djokovic was attempting to extend his 35-match winning streak against 14th-seeded Stanislas Wawrinka.
Fourth-ranked Andy Murray advanced with a 6-2, 6-3 win over Potito Starace of Italy, and will next meet Florian Mayer of Germany, who beat Juan Ignacio Chela 7-5, 6-4.
Gasquet's quarterfinal opponent will be seventh-seeded Tomas Berdych, who held off Finnish qualifier Jarkko Nieminen 6-2, 6-7 (4), 6-2.
This tournament is a key warmup event for the French Open, which starts in 10 days, and it is one of only three Masters Series events that Federer has never won.
Federer didn't seem to think the loss would affect his performance at Roland Garros.
"I'll definitely be happy to take two, three, four days off to get mentally fresh and then practice very hard," he said. "There's no reason to panic."
It looked like Federer was going to advance easily when he won 10 straight points to open the encounter, but the 16-time Grand Slam winner squandered a 4-2 lead in the second set, then failed to convert two break points that would have given him a chance to serve out the match.
Federer missed several backhands in the second-set tiebreaker and floated another backhand long on Gasquet's first match point in the deciding tiebreaker.
In all, Federer committed 30 unforced errors to Gasquet's 19. "I had multiple chances," Federer said. "It's disappointing. ... I probably never felt I was going to win the breaker it''s tough to play that way."
The match was delayed for about 10 minutes early in the second set when water mysteriously began leaking onto the corner of the court.
In women's action on a cloudless day at the Foro Italico, French Open champion Francesca Schiavone roused her home crowd by rallying to beat Daniela Hantuchova of Slovakia 3-6, 6-2, 7-5.
Schiavone is ranked a career-high fourth, but is seeded higher this week since No. 2 Kim Clijsters is injured and No 3 Vera Zvonareva didn't enter.
With Serena and Venus Williams also out injured, local fans are hoping Schiavone can become the first Italian to win this tournament since Raffaella Reggi in 1985.
After Schiavone chased down a drop shot and landed a winner up the line to secure the victory, organizers blasted the old Neapolitan folk song "O surdato ''nnammurato" over the loudspeakers and nearly all of the 10,500 fans in attendance sang along.
Schiavone's quarterfinal opponent will be Sam Stosur in a rematch of last year's Roland Garros final.
The sixth-seeded Stosur beat Slovenian qualifier Polona Hercog 6-3, 6-4.
"They key against (Stosur) is always the same, you've got to stay inside the court and take control," Schiavone said.
"Whoever takes the initiative wins. I've beaten her both in Paris and in Fed Cup, but the matches have always been very balanced."
Top-ranked Caroline Wozniacki eliminated Yanina Wickmayer 6-1, 7-6 (4) and will next meet two-time Rome champion Jelena Jankovic, a 6-4, 2-6, 6-2 winner over Spanish qualifier Anabel Medina Garrigues.
Also, Australian Open runner-up Li Na of China rolled past Jarmila Gajdosova of Australia 6-2, 6-1 and seventh-seeded Maria Sharapova dispatched 10th-seeded Shahar Peer 6-2, 6-2.