It was the reverse of last year's final, when Federer won his fourth title in Hamburg and ended Nadal's 81-match winning streak on clay.
Nadal rallied from big deficits in the first two sets, although he lost the tiebreaker in the second. He led 4-1 in the third and held on to raise his record against the top-ranked player to 8-1 on clay and 10-6 overall.
"All the week has been very special for me," Nadal said.
The second-ranked Spaniard has 26 titles, 21 of them on clay, including the last three French Opens. Nadal also won in Monte Carlo -- beating Federer in the final -- and Barcelona, Spain, this year.
"It was a strange match," Nadal said. "Roger made some mistakes in the first set that helped me. . . . It was important to win because it was the last big clay-court tournament I never won."
Federer lost his seventh match of the year and has only one title so far, at a relatively minor clay-court tournament in Estoril, Portugal.
The Hamburg Masters is a major warmup tournament for the French Open, the only Grand Slam tournament that Federer has not won.
"I could have served a little better; it wasn't my best performance, maybe. I have to go for big serves -- he is a good return player," Federer said. "It was a fun match."
Federer went into Sunday's final with a 41-match winning streak in Germany and a 9-0 record in finals on German soil. His last loss in Germany had been in 2003.
The fourth-seeded Jankovic took control early against the 18-year-old Cornet, who hurt herself with repeated unforced errors and converted only three of 12 break points. Cornet, the first female qualifier to reach the final of the Italian Open in decades, broke into tears during a second-set changeover.
Jankovic was leading 4-1 in the second set when she called the trainer to look at her right shoulder. Jankovic lost the following game -- the first time in the match Cornet held serve -- but went on to win the match as she held serve and then broke Cornet's serve at love.
Cornet, last year's French Open junior champion, has also reached two clay-court semifinals in the United States.
The third-seeded UCLA men's team defeated sixth-seeded USC, 4-2, in the quarterfinals of the NCAA Championships in Tulsa, Okla.
The Bruins (25-1) will play seventh-seeded Texas today in their first semifinal since winning the national championship in 2005.
UCLA prevails, 4-3, to reach super regional
Freshman Samantha Camuso hit a go-ahead home run to lead off the sixth inning, her second homer of the regional, as UCLA (48-7) advanced to super regional play with a 4-3 victory over Nevada (44-18) at Easton Stadium.
Second-seeded UCLA will play 15th-seeded Georgia next weekend.
Camuso was two for three with two runs batted in.
Senior Anjelica Selden improved to 26-3, striking out seven and giving up no walks, five hits and three runs in a complete game.
Russia beats Canada for first title since '93
Ilya Kovalchuk scored his second goal of the game in overtime, giving Russia its first world hockey title since 1993 with a 5-4 victory over Canada in Quebec City.
Canada took a 4-2 lead into the third period but couldn't hold off the Russians. Alexei Tereshchenko and Kovalchuk scored to tie it, setting the stage for the overtime winner.
Rick Nash was sent to the penalty box after accidentally sending the puck over the glass from his own end. The Russians then put out four forwards for the four-on-three advantage, and Kovalchuk beat Canadian goalie Cam Ward with a wrist shot at 2:42 of the extra period, ending a 17-game winning streak for Canada dating to last year's gold-medal win.