Euro 2008 co-hosts Switzerland became the first team knocked out of the competition after they lost to Turkey.
The Swiss had taken the lead during a torrential downpour when a low cross slowed on the drenched pitch and allowed Hakan Yakin to tap in.
Yakin missed a glorious chance from the same spot and was punished later on when Semih Senturk headed Turkey level.
Arda Turan then broke Swiss hearts when he cut inside and scored with a shot in injury-time which took a deflection.
Portugal's win over the Czech Republic earlier on had added to the pressure on the home side as they knew a defeat would result in their elimination.
The situation meant the St Jakob-Park stadium in Basel was a melting pot of nervous tension and passionate support from the Swiss contingent at the start.
There was also an extra element of needle around the game after the two teams were involved in a mass brawl at the final whistle of a World Cup 2006 play-off which the Swiss won on away goals.
Turkey's hunger for revenge saw them look the more assured in the early stages, while the prospect of elimination weighed heavily on a lacklustre Switzerland.
The pouring rain did not help the game and the home nation had to produce some last-ditch defending before giving their crowd reason to get excited.
Yakin's stinging shot was palmed away by Volkan Demirel before the Turkey keeper excellently touched a Tranquillo Barnetta free-kick round his post.
At the other end, Turkey hit the post when keeper Diego Benaglio pushed a free-kick inadvertently against Arda Turan and the ball richocheted back off the woodwork.
The weather played a big part at the match in Basel
The downpour had meant that the ball regularly held up on the drenched pitch and, although the atrocious conditions made the game almost impossible, this worked to Switzerland's favour.
Philippe Senderos played a pass over the top of Turkey's defence to Derdiyok and he, with the help of the soaked ground, gave Yakin the simple task of tapping in.
Yakin had another glorious chance at the same spot minutes later when Valon Behrami crossed but the Swiss playmaker sidefooted the bouncing ball unbelievably wide from point-blank range.
Turkey came out with a renewed sense of purpose after the break as the rain ceased and they grabbed an equaliser when Semih Senturk nodded in Nihat's cross.
Swiss keeper Benaglio got two hands to the header but, instead of saving, could only divert the ball into his goal.
Nihat was inches away from a Tuncay Sanli cross as Turkey threatened a winner which finally came in dramatic fashion late on.
Turkey now face the Czech Republic with both sides on three points and in a straight shoot-out for a last eight slot, while Portugal have now qualified.
Switzerland: Benaglio, Lichtsteiner, Muller, Senderos, Magnin, Behrami, Inler, Gelson (Cabanas 76), Barnetta (Vonlanthen 66), Yakin (Gygax 85), Derdiyok.
Subs Not Used: Zuberbuhler, Jakupovic, Djourou, Huggel, Frei, Streller, Grichting, Spycher, Degen.
Goals: Yakin 32.
Turkey: Demirel, Altintop, Asik, Cetin, Balta, Karadeniz (Senturk 46), Aurelio, Metin (Topal 46), Turan, Nihat (Kazim-Richards 85), Sanli.
Subs Not Used: Rustu, Zengin, Zan, Emre, Gungor, Boral, Akman, Sarioglu, Erdinc.
Booked: Sanli, Aurelio, Balta.
Goals: Senturk 57, Turan 90.
Ref: Lubos Michel (Slovakia).