Roddick broke serve only once but held every service game in the first and last sets. After falling behind 15-30 in the final game, he closed out the victory with an ace and two service winners.
"There is no script in sports, you know. I think that's what makes it the best entertainment in the world," Roddick said. "Nights like tonight are why you play the matches. You don't know what's going to happen."
On paper, it looked like a mismatch between former No. 1s, each a two-time champion in the event. The third-ranked Federer began the night 40-2 since the U.S. Open, while Roddick was ranked 34th, the lowest he has been since 2001.
Roddick ended Federer's streak of 77 consecutive wins against players outside the top 20.
"I feel like I lost against a former No. 1, not that I lost against a guy ranked 30 in the world," Federer said. "I'm happy to see Andy play really well. He's a great champion, and enjoy him while you have him. It was a great night for him and America's tennis."
Also dominating with the serve were Serena and Venus Williams, who both reached the quarterfinals.
Serena matched a career high with 20 aces to beat Samantha Stosur 7-5, 6-3.
"My serve was hot," Williams said. "I was like, 'That's pretty cool.'"
She avenged a loss to Stosur when they last met in the U.S. Open final in September.
"That wasn't in my mind," Williams said. "I just thought, 'This is a new game.'"
Older sister Venus won her third consecutive three-set match, hitting 13 aces as she outlasted No. 15-seeded Ana Ivanovic 6-7 (4), 6-2, 6-2. Williams is playing in her first tournament since withdrawing from the U.S. Open last August after being diagnosed with an autoimmune disease.
"I come out on court knowing I can play well, but almost with no expectations," Williams said.
Top-ranked Victoria Azarenka lost 10 of the first 11 games, then rallied to remain unbeaten this year by overcoming Dominika Cibulkova 1-6, 7-6 (7), 7-5 in the fourth round.
Azarenka won her 26th consecutive match, a streak that began at the start of this year. Her start is the best on the women's tour since Martina Hingis went 37-0 to begin 1997.
In the second set, Azarenka fell behind 4-love. She found herself two points from defeat five times but swept the last three games and converted the first match point when Cibulkova sailed a passing shot long.
Azarenka has won her four previous tournaments this year, including the Australian Open for her first Grand Slam title. She seeks her third Key Biscayne title.
Also reaching the quarterfinals was No. 2 Maria Sharapova, who overcame 11 double-faults to beat Ekaterina Makarova 6-4, 7-6 (3). Sharapova won despite committing 52 unforced errors and losing her serve four times.
No. 1 Novak Djokovic advanced to the men's fourth round by beating No. 27-seeded Viktor Troicki 6-3, 6-4. Djokovic is bidding for his third Key Biscayne title and second in a row.
No. 8-seeded Mardy Fish assured he'll remain the top-ranked American man by beating No. 28 Kevin Anderson 6-4, 6-3. Fish has yet to play in the stadium this tournament and instead faced Anderson at 11 a.m. on the grandstand court, but he said that was fine because prefers an early start.
In other women's play, former No. 1 Caroline Wozniacki defeated No. 23 Yanina Wickmayer 7-6 (6), 6-0. Reigning French Open champion Li Na rallied past Sabine Lisicki 3-6, 6-4, 6-2, and No. 5 Agnieszka Radwanska defeated 18-year-old Garbine Muguruza Blanco 6-3, 6-2.
The No. 10-seeded Williams lost only six points on her first serve against the No. 6-seeded Stosur, but had to rally after being broken in the first game of each set.
Williams failed to convert her first seven break-point chances, then muscled her way back into the match. She also showed finesse, such as when she chipped a forehand drop shot that barely cleared the net for a dainty winner.
Williams is playing in her first tournament since January after being sidelined by a left ankle injury. She hasn't won a title since August, and seeks a record sixth Key Biscayne championship.