The 37-year-old Swiss had prioritised winning a sixth title at Flushing Meadows but was beaten 3-6 7-5 7-6 (9-7) 7-6 (7-3) in three hours 35 minutes.
It is the first time Federer has lost to a player ranked outside of the top 50 at the US Open.
Australian Millman will play two-time champion Novak Djokovic in his first Grand Slam quarter-final on Wednesday.
Millman, 29, is the only unseeded player left in the last eight of the men’s draw.
“I’m probably in a little bit of disbelief,” said Millman.
“I have so much respect for Roger and everything he’s done for the game. He’s been a hero of mine.”
Second seed Federer, bidding for a record-extending 21st Grand Slam, missed set points in the second and third sets before capitulating in the fourth-set tie-break.
Millman had never beaten a top-10 player before but suddenly found himself with five match points against arguably the greatest male player of all-time.
After Federer rallied to save two of them, Millman completed victory at almost 01:00 local time in a humid New York when the out-of-sorts Swiss lumped a forehand long.
It was Federer’s first defeat in 41 matches at the US Open against a player ranked outside the top 50.
Federer, usually so calm and collected, was flustered throughout most of the match as he complained about crowd noise and became unusually vocal during some tense moments.
“It was very hot and one of those nights where I felt I couldn’t get air,” he said.
“I just struggled in the conditions and it was one of the first times it has happened to me.
“You just keep sweating and lose energy. At some point I was just happy the match was over.”(BBC)