Millman's 3-6, 7-5, 7-6 (9/7), 7-6 (7/3) triumph scuppered a blockbuster last-four meeting between Federer and two-time US Open champion Djokovic, who had advanced with a 6-3, 6-4, 6-3 victory over Joao Sousa.
But Federer suffered his earliest US Open exit since he fell in straight sets to Tommy Robredo in the fourth round in 2013.
It was also 20-time major winner Federer's first loss against a player outside the top 50 in 41 meetings at the US Open.
"I'm probably in a little bit of disbelief," Millman, 29, said after three hours and 35 minutes in the cauldron of Arthur Ashe Stadium.
"I have so much respect for Roger and everything he's done for the game. He's been a hero of mine."
Federer had set points in both the second and third sets, but troubles with his serve infected his entire game and 77 unforced errors doomed the 20-time Grand Slam champion in the face of a determined attack from Millman.
"It was very hot," said Federer, whose shirt and shorts were wringing wet with sweat most of the night.
"I felt I couldn't get any air and I was sweating more and more and became more and more uncomfortable.
"John dealt with it better," Federer added. "He's from Brisbane, one of the most humid places in the world!"
Millman said he was feeling the nerves of his first Grand Slam fourth round match as he surrendered the first set with a single break of serve.
Although Federer broke first on the way to a 5-3 lead in the second, Millman won the last four games, breaking Federer twice as Federer wasted two set points in the 10th game.
Federer upped his game in a tight third set, and had the first chance to claim the set at 6-5 in the tiebreaker. He netted a service return, and Millman converted his second set point to seize a two sets to one lead.
When the Swiss great broke for a 4-2 lead in the fourth the Ashe fans were on their feet.
But he gave the break back in the next game after slamming an overhead into the bottom of the net on game point.
They went to the tiebreaker, where two of Federer's 10 double faults, followed by a backhand into the net and a wayward forehand gave Millman a 6-1 lead.
Federer, looking all of his 37 years, clawed his way through two match points before slapping yet another forehand wide as Millman celebrated.
"Last time I played him I got three games at Queens," Millman said. "Let's hope I get a few more."