Ferrari dominated in practice and Charles Leclerc was a heavy favourite to go fastest at the Hockenheimring.
However, he was unable to take part in the final session while team-mate Sebastian Vettel had a nightmare as a power supply issue means he will start the race dead last.
Hamilton capitalised to claim pole with Red Bull’s Max Verstappen second and Mercedes team-mate Valtteri Bottas in third.
Hamilton said he was delighted with such a good result in his team’s home country but admitted he was surprised by Ferrari’s struggles.
“I’m not really sure how we did it today, and I’m not really sure what happened to the Ferraris,” he said.
“But it’s such an important race for us, our second home Grand Prix, and 125 years of motor racing, so it’s incredible to celebrate in this way.
“We’ve brought some upgrades this weekend, and the car has felt really good. Ferrari have been really fast this weekend, and I think they’ve been on a slightly different level.
“I think the times at the end were good enough to be able to compete with Leclerc if he’d done a lap at the end. It would’ve been close between us.”
Mercedes boss Toto Wolff echoed Hamilton’s words and said it was unfortunate for fellow German Vettel.
“It is a shame for Ferrari and for Sebastian at his home GP,” Wolff said.
“Ferrari have an illness that they need to cure and we need them so we can have a competitive championship.
“We do not know why we started to lack pace at the start. But Lewis put in an incredible lap and also Valtteri has a great chance in the race.”
How they will start in Germany
1 Hamilton (Mercedes)
2 Verstappen (Red Bull)
3 Bottas (Mercedes)
4 Gasly (Red Bull)
5 Raikkonen (Alfa Romeo)
6 Grosjean (Haas)
7 Sainz (McLaren)
8 Perez (Racing Point)
9 Hulkenberg (Renault)
10 Leclerc (Ferrari)
11 Giovinazzi (Alfa Romeo)
12 Magnussen (Haas)
13 Ricciardo (Renault)
14 Kvyat (Toro Rosso)
15 Stroll (Racing Point)
16 Norris (McLaren)
17 Albon (Toro Rosso)
18 Russell (Williams)
19 Kubica (Williams)
20 Vettel (Ferrari)