Toni Kroos scored an injury time goal as Germany managed to see off a very resilient Sweden side in their second group match in Sochi.
Ola Toivonen’s deft first-half lob had left Joachim Low’s side facing elimination but Marco Reus, starting in place of Arsenal’s Mesut Ozil, prodded in an equaliser as Germany assumed control after the interval.
The champions then had Jerome Boateng sent off late on for a second yellow card but continued to chase the game knowing a draw left their fate in others’ hands.
And they were rewarded in the final minute of five added on when Kroos swept a free-kick into the top right corner from the left of the penalty area.
The magnitude of the moment saw the euphoric German players run to rejoice in front of their own fans while the Sweden players slumped to the floor, some close to tears.
There were also angry exchanges in the technical area with Sweden’s manager Janne Anderson upset by the exuberance of the celebrations in front of him.
The result means all four teams in Group F can still qualify for the next phase, with the outcome to be decided at venues 600 miles apart on Wednesday 27 June (15:00 BST) when Mexico face Sweden in Ekaterinburg and Germany play South Korea in Kazan.
At half-time it looked as though it was to be a black night for Germany in Sochi, the southernmost venue at the tournament, over 1,000 miles from Moscow, on the coast of the Black Sea.
It seemed perfectly possible that Germany could lose back-to-back games at a finals for the first time since 1958 and become the fourth champions in the past five tournaments to exit at the first-round stage.
But with Low urging his side on from the technical area there was a noticeable increase in tempo following the introduction of Mario Gomez at the interval.
And when Gomez was unable to convert Timo Werner’s cross, Reus followed up to level the score, restore some belief to the rattled Germans and visibly relieve the tension on manager and supporters inside the Fisht Olympic Stadium.
How Sweden had Germany on the brink
Static defending was a characteristic of Germany’s opening defeat by Mexico, who cut through and round them with nimble interchanges and one-touch play.
While the Germany coach made four changes, including two to his back four, the same pattern was in evidence against the Swedes.
Emil Forsberg’s exquisite turn left the entire German backline floundering early on and moments later Marcus Berg raced clear only to be pulled back by Boateng as he bore down on goalkeeper Manuel Neuer, the Swedes complaining that they should have had a penalty.
Germany’s deficiencies only grew more apparent, with Kroos sloppily giving away the ball inside his own half to start the move that led to Sweden’s goal.
Viktor Claesson’s pass found Toivonen and suddenly Germany appeared on the brink of exiting the World Cup at the first-round stage for the first time since 1938.
Never know when they’re beaten?
Much of the praise for Germany’s turnaround must go to Low, who boldy dropped Ozil at a major international tournament – for the first time since the 2010 World Cup in South Africa – and then watched his replacement, Reus, breathe life into Germany’s campaign.
While born of necessity, the introduction of Julian Brandt for Jonas Hector late on, effectively leaving a side with down to 10 men with just one defender on the pitch, was also to the coach’s credit.
Although Low’s decision to include Brandt in his 23-man squad at the expense of Manchester City’s Leroy Sane caused a stir in the media, when he came on the Bayer Leverkusen winger struck the post with a fierce effort and injected impetus before Kroos atoned for his earlier error.
It proved quite a turnaround from the team meetings, rows and recriminations within the German camp in the days following their 1-0 defeat by Mexico.