The reigning world champions had already sealed their ticket to Russia last Thursday with a 3-1 win over Northern Ireland, and finished their qualifying campaign in Kaiserslautern with a 10th straight victory.
This is the first time Germany have qualified for a World Cup finals with a perfect record.
Joachim Loew’s side are only the second team to achieve the feat in a European qualifying campaign after Spain managed it on their way to winning the 2010 World Cup in South Africa.
However, the comfortable win came at the cost of two centre-backs, as Bayern Munich’s Niklas Suele and Arsenal’s Shkodran Mustafi both limped off with first-half injuries.
Schalke midfielder Goretzka gave Germany an early lead with a superb back-heeled goal before Azerbaijan striker Ramil Sheydaev stunned the hosts by equalising before half-time.
Second-half strikes from Hoffenheim striker Sandro Wagner and Chelsea defender Antonio Ruediger, before Goretzka added his second, saw Germany pull away.
Liverpool midfielder Emre Can added the fifth when he smashed home his first international goal with a powerful shot from outside the area.
The World Cup holders lost Julian Draxler before kick-off as the Paris Saint-Germain midfielder dropped out with flu, after Real Madrid star Toni Kroos had already been ruled out with a rib injury.
The hosts got off to a dream start when Goretzka showed great technique by turning the ball into the net on eight minutes.
Then came Germany’s injury blows.
Suele limped off on 22 minutes to be replaced by Chelsea’s Ruediger before Mustafi went down in the build-up to the equaliser.
The Germans should have doubled their lead with half an hour gone when Wagner hit the post.
Azerbaijan deservedly levelled on 34 minutes when Sheydaev sprinted clear of Mustafi — who pulled up with a leg injury.
Sheydaev then wrong-footed Ruediger and fired past Germany goalkeeper Bernd Leno to stun the hosts.
It stayed 1-1 at the break, but Wagner put Germany back in the lead on 54 minutes thanks to Hawk-Eye.
Wagner’s header was cleared in a scramble, but goal-line technology showed the ball had crossed the line for his fifth international goal in as many games.
At 2-1 up, the floodgates opened when Ruediger hammered his header past Azerbaijani goalkeeper Kamran Agayev on 64 minutes.
Goretzka grabbed Germany’s fourth just two minutes later when he fired home Leroy Sane’s pass after some good work by the Manchester City winger.
The 23-year-old Can completed the scoring in style when he rifled a 30-yard strike into the roof of the net.
With time almost up, Azerbaijan defender Magomed Mirzabekov hit the post and Germany’s Matthias Ginter cleared a shot off the line.