Toni Kroos' injury-time winner against Sweden revived defending champions Germany's hopes of becoming the first side to retain the World Cup since Brazil in 1962.
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 tapped a free-kick short to Reus before sweeping the return pass into the top right corner from 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.
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.
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.
Copenhagen goalkeeper Robin Olsen impressed for Sweden but couldn't add to five consecutive international clean sheets on his 20th appearance for his country
Germany boss Joachim Low: "This was a thriller, full of emotion, right up until the final whistle. Brandt hit the goal post just three minutes before the end too. We took out a defensive player and brought on an attacking player because we knew had to bring on everything we had to turn it round.
"We had a couple of great chances - Mario Gomez's header being one of them. The last couple of minutes were full of drama but those matches exist in football. We've had these situations in other tournaments as well. For the viewers that's part of the attractiveness of football.
"Something I did appreciate today was that we didn't lose our nerve, we didn't panic after going a goal down. We never lost hope we could win the match and I think the goal scored in stoppage time had a bit of luck involved but it did show the belief we had in ourselves."
Sweden manager Janne Andersson: "They didn't create that many good opportunities, our keeper did make a few good saves, but I feel unlucky we didn't get at least one point.
"But I'm not blaming anyone or any tactical decisions. This is probably the heaviest conclusion to a match I've had in my career - but the whole group is still alive so we'll have to lick our wounds and come back for the next match.
"We had the odd opportunity but we kept the match under control up until the free-kick. It was an extraordinary individual goal. We had to work very hard throughout and once you get to the 85th minute you are very tired."
Germany play South Korea in Kazan in their final Group F match at 15:00 BST on Wednesday, with Mexico facing Sweden in Ekaterinburg at the same time.