World number one Federer eased past unseeded Italian Andreas Seppi 6-3 6-1 in the other semi-final.
Djokovic has won the Australian Open and two Masters Series already this year and would have taken over Nadal's number two world ranking if he had beaten the French Open champion.
With the retractable roof at the Rothenbaum closed to keep out the rain, there was an extra buzz around the centre court and Nadal and Djokovic wowed the crowd with some awesome rallies.
The fist-pumping Mallorcan, seemingly able to chase down almost anything Djokovic threw at him, saved 15 of 19 break points against him and broke serve in the first and seventh games of the deciding set.
He sealed victory on his fifth match point when Djokovic netted an attempted drop shot at the end of an incredible final game lasting more than 15 minutes.
"It was an amazing match," Nadal said. "I am a little bit tired right now so let's see how I feel tomororow."
Hamburg is the only Masters Series title on clay that eludes Nadal and he said the slower court here took some of the sting out of his booming top-spin drives.
"I'll need to be 100 percent if I want to have a chance of beating Roger," he added.
Djokovic said the semi-final had possibly been his best performance to date on clay against a man he described as the greatest defensive player in tennis history.
"It was unfortunate it finished as a loss for me but I have to take the positives out of the match," he said.
"I feel that with this performance and the match in general I am getting closer to him on clay and hopefully next time I can get a win."
In Saturday's first semi-final, world number one Federer was only once under pressure on his serve and closed out the win in 79 minutes.
The 26-year-old Swiss played some fabulous points, a drop-shot return and a whipped crosscourt forehand winner on the run the pick of the bunch.
Federer said he was very happy to be back in the Hamburg final with the French Open starting at the end of this month, the only grand slam he has failed to win.
He fought back from a set down to beat Nadal last year, breaking the Spaniard's 81-match winning streak on clay, but lost to him in the final at Roland Garros for a second straight year.
"Maybe I didn't serve my best today but I was very solid and consistent from the baseline and played a very good match," Federer said.
Nadal, a three-times French Open winner, has spent a record 147 weeks ranked second behind Federer dating back to July 2005.
He had won all three of his previous matches against Djokovic on clay, beating the Serb in straight sets in the semi-finals at Roland Garros last year.