The world number one looked to be heading towards a certain defeat after losing the opening two sets but clawed back to edge Federer 6-7 4-6 6-3 6-2 7-5 and remain on course for his third grand slam title this year after winning the Australian Open and Wimbledon championships.
For Federer, who was chasing a record sixth U.S. Open title in eight years, it was a heartbreaking loss and only the second time in 184 matches that he has been beaten after leading by two sets. The only other time was at Wimbledon in July.
It also ended his streak of winning at least one grand slam every year since he won the first of his 16 majors in 2003 and the Swiss master cut a forlorn figure as he trudged off the Arthur Ashe Stadium court.
Djokovic will now play Rafa Nadal in Monday's final, which was shifted from Sunday after two days of rain forced a schedule change.
Despite losing the third and fourth sets, Federer still had a chance to win the match after breaking Djokovic's serve to lead 5-3. He looked to have the match in the bag when he led 40-15 on serve but squandered his two match points and lost the last four games.
The start of the match was delayed over an hour because of a passing shower but the players made up for lost time with a scintillating first set full of unrelenting tension and precision serving.
The first 13 points all went with the server and neither player faced a break point in the first 12 games as the set went to a tiebreaker.
Federer seized the early initiative but squandered four set points, including three in a row, before sealing the set after 55 minutes when Djokovic dumped a forehand into the net.
The raucous center court crowd roared their approval as the players retreated to their chairs like a pair of prize fighters waiting for the bell to start the next round of what proved to be titanic battle between two of the game's heavyweights.
Federer, playing his first grand slam as a 30-year-old, landed the first punch of the second set when he broke Djokovic's serve in the third game.
The Serbian broke back for 3-3 when Federer ballooned a forehand over the baseline then hit the next one wide but failed to consolidate the break, losing his next game to love.
The frustration was beginning to show on Djokovic, who entered Saturday's match having lost just twice all year, and there were some early signs of fatigue after a long season that has brought him nine titles.
He retired from last month's Cincinnati Masters final because of a sore shoulder and bent down to massage his right ankle on Saturday before conceding the second set when he slapped a return into the net but it was a false alarm.
Djokovic came out swinging in the third set and jumped to a decisive 3-0 lead after finally discovering a way to break Federer's impenetrable serve, pinning the third seed further behind the baseline as he started to find his rhythm.
The 24-year-old suddenly seemed re-energised as Federer started to tire in the humidity, gifting points to Djokovic with shanks and shots off the frame.
Djokovic raced through the fourth set in 33 minutes, the quickest of the match, as the crowd rose to their feet to urge Federer to raise his game for the deciding set.
The former world number one responded, holding his first four service games before breaking Djokovic in the eighth but his own serve let him down when he needed it most.