Nadal squandered a 3-1 lead in the final set and saved two set points before prevailing in 2 hours and 34 minutes over the eighth-seeded Italian and the 2013 Hamburg winner.
"It was a great match, many great points, many breaks, we fought hard," Nadal said. "I had many ups and downs."
Former top-ranked Nadal extended his run of having won at least one European clay-court title every year since 2004 when he took his maiden crown in Sopot, Poland.
"It's an important title for me," Nadal said.
Nadal now has 47 clay-court titles, second to Guillermo Vilas' 49. It was Nadal's second title on clay this year after Buenos Aires in February. The Spaniard has 67 career titles overall. Nadal won in Hamburg in 2008.
With his third title of the year, Nadal will rise one spot to No. 9 in the rankings.
Fognini was trying to become the first player to beat Nadal three times on clay in a year. With six losses this year, Nadal has as many defeats on clay as in three previous years combined.
The two players traded breaks in the first four games and Nadal saved one break point to hold for 6-5. Fognini then saved three break points before Nadal clinched the set with a forehand return.
Nadal served for a 4-1 lead in the second, but Fognini turned the match before wasting two set points in the 10th game. Nadal again had to save two break points before going 6-5 up. Two successive forehand errors by Fognini gave Nadal the match in the next game.