Late rain over Hockenheim caused chaos and Germany's Vettel slid his Ferrari off the track on the 52nd of 67 laps of the 4.574-kilometre circuit. It not only cost him the win but also the lead in the world championship.
After the resulting safety car came in with 10 laps remaining, Hamilton found himself in the lead and held off an attack from team-mate Valtteri Bottas, before the Finn was ordered to hold his position in second. Kimi Raikkonen was third in the other Ferrari.
Hamilton started 14th after a hydraulic failure in qualifying but his astonishing comeback returns him to the top of the standings on 188 points to Vettel's 171.
“It’s obviously highly unlikely from that position - but you’ve always got to believe," Hamilton said straight after the race.
"The team did a fantastic job today. I’m so grateful. Hopefully this solidifies be belief. For those who don’t know me, now they do.”
Vettel started well from pole position and looked set for a first F1 win in Hockenheim, having taken his previous German GP victory at the Nuerburgring, as he led with 15 laps remaining.
Raikkonen was on a different pit strategy and when he was ordered aside for the quicker Vettel, the home favourite seemed set until rain brought an astonishing conclusion.
It had not been wet enough to merit an immediate change of tyres and when Vettel went wide at the Saxe turn, he could not recover and drove into the barrier.
He could only apologize over the radio to his team and sounded close to tears.
Bottas dove into the pits with his team seemingly scrambling for the right tyres, as the rain eased again, and when Raikkonen followed Mercedes had a decision to make on Hamilton.
The team called him in but Hamilton aborted the stop at the last second, cutting back across the grass at the pit lane to remain out.
"Conditions were perfect for business time," Hamilton said. "When it rained, I knew I would have a good position."
Bottas had a serious look at taking the lead on lap 58 as the sprint to the finish began but his Mercedes team decided their drivers competing was too risky. They promptly ordered him to back off and he accepted with grace.
"Taking positives, as a team it is a perfect result for us," Bottas said.
"We had a bit of a battle lap one after the safety car, I didn't get past and I got told to minimize the risk but I understand."
That ensured Hamilton his 67th career win and a remarkable turnaround, not only from Saturday but the two previous races which ended badly with him losing ground to Vettel.
He now takes a 17-point lead into the Hungarian Grand Prix next Sunday, the last race before the summer break.
"It's motor racing," Mercedes team chief Toto Wolff told Sky Sports. "The race happens on Sunday and not on Saturday. All the bad luck we had came back to us in tremendous good luck."
Max Verstappen of Red Bull was fourth having gambled by changing his tyres only for the rain to disappear and Nico Huelkenberg of Renault was fifth.
Romain Grosjean (Haas), Sergio Perez, Esteban Ocon (both Force India), Marcus Ericsson (Sauber) and Brendon Hartley (Toro Rosso) completed the top 10.
Kevin Magnussen (Haas), Carlos Sainz (Renault), Stoffel Vandoorne (McLaren), Pierre Gasly (Toro Rosso), Charles Leclerc (Sauber) and Fernando Alonso (McLaren) were behind them.
In addition to Vettel, Lance Stroll, Sergey Sirotkin (both Williams) and Daniel Ricciardo, who started his Red Bull from 19th following penalties for engine changes, failed to finish.
After the resulting safety car came in with 10 laps remaining, Hamilton found himself in the lead and held off an attack from team-mate Valtteri Bottas, before the Finn was ordered to hold his position in second. Kimi Raikkonen was third in the other Ferrari.
Hamilton started 14th after a hydraulic failure in qualifying but his astonishing comeback returns him to the top of the standings on 188 points to Vettel's 171.
“It’s obviously highly unlikely from that position - but you’ve always got to believe," Hamilton said straight after the race.
"The team did a fantastic job today. I’m so grateful. Hopefully this solidifies be belief. For those who don’t know me, now they do.”
Vettel started well from pole position and looked set for a first F1 win in Hockenheim, having taken his previous German GP victory at the Nuerburgring, as he led with 15 laps remaining.
Raikkonen was on a different pit strategy and when he was ordered aside for the quicker Vettel, the home favourite seemed set until rain brought an astonishing conclusion.
It had not been wet enough to merit an immediate change of tyres and when Vettel went wide at the Saxe turn, he could not recover and drove into the barrier.
He could only apologize over the radio to his team and sounded close to tears.
Bottas dove into the pits with his team seemingly scrambling for the right tyres, as the rain eased again, and when Raikkonen followed Mercedes had a decision to make on Hamilton.
The team called him in but Hamilton aborted the stop at the last second, cutting back across the grass at the pit lane to remain out.
"Conditions were perfect for business time," Hamilton said. "When it rained, I knew I would have a good position."
Bottas had a serious look at taking the lead on lap 58 as the sprint to the finish began but his Mercedes team decided their drivers competing was too risky. They promptly ordered him to back off and he accepted with grace.
"Taking positives, as a team it is a perfect result for us," Bottas said.
"We had a bit of a battle lap one after the safety car, I didn't get past and I got told to minimize the risk but I understand."
That ensured Hamilton his 67th career win and a remarkable turnaround, not only from Saturday but the two previous races which ended badly with him losing ground to Vettel.
He now takes a 17-point lead into the Hungarian Grand Prix next Sunday, the last race before the summer break.
"It's motor racing," Mercedes team chief Toto Wolff told Sky Sports. "The race happens on Sunday and not on Saturday. All the bad luck we had came back to us in tremendous good luck."
Max Verstappen of Red Bull was fourth having gambled by changing his tyres only for the rain to disappear and Nico Huelkenberg of Renault was fifth.
Romain Grosjean (Haas), Sergio Perez, Esteban Ocon (both Force India), Marcus Ericsson (Sauber) and Brendon Hartley (Toro Rosso) completed the top 10.
Kevin Magnussen (Haas), Carlos Sainz (Renault), Stoffel Vandoorne (McLaren), Pierre Gasly (Toro Rosso), Charles Leclerc (Sauber) and Fernando Alonso (McLaren) were behind them.
In addition to Vettel, Lance Stroll, Sergey Sirotkin (both Williams) and Daniel Ricciardo, who started his Red Bull from 19th following penalties for engine changes, failed to finish.