Nadal, playing in his first tournament since Wimbledon, had already lost to Fognini twice on clay this year.
But he won a tight final that saw 12 breaks of serve to extend his run of having won a European clay-court tournament every year since 2004.
It is the 67th singles title of the 29-year-old's career and his third in Hamburg, where he last won in 2008.
"It was a very tough final, it is an important title for me" said Nadal, who suffered cramp as he tried to conduct a post-match interview on the court.