He was India captain Rohit Sharma's opening partner in the home T20 series against Afghanistan earlier this year but his place looked under threat as he struggled for runs in the IPL
Hungary looked like they would shock Germany and send them out of the tournament after Adam Szalai's diving header in the 11th minute opened the scoring.
Kai Havertz drew Germany level in the 66th minute but Hungary retook the lead seconds after the re-start when András Schäfer ran past the defenders and headed the ball into the net.
But with coach Joachim Loew throwing on substitutes to gain the point needed to progress to the last 16, Goretzka's strike provided Germany with the late life-line.