Brazil, who needed a win to go through, took a fully-deserved first half lead through Adriano, but were rocked by Gokdeniz Karadeniz's equaliser in the 53rd minute and pole-axed by an 81st minute strike by substitute Okan Yilmaz.
Brazil substitute Alex equalised in the third minute of stoppage time, but they had Ronaldinho dismissed for a second yellow card in the closing seconds -- for wrestling with Turkey keeper Rustu Recber for the ball after Alex's goal.
Both teams finished with four points and the same goal difference but Senol Gunes's side went through on goals scored -- four to Brazil's three.
Group B winners Cameroon, who drew 0-0 with the United States, will play Colombia in the other semi-final, with both games being played on Thursday.
In a passionate match, watched by an almost entirely Turkish 30,000 crowd that booed Brazil from start to finish, Turkey finally got their revenge for being beaten by Brazil at the group stage and the semi-finals of the 2002 World Cup.
It was richly deserved for their ability to slice the South Americans apart on the break in the second half.
But Brazil, who lost their opening game 1-0 to Cameroon, will be kicking themselves after squandering the chance to be three goals in front by halftime.
The result also denied the neutrals a re-match of the 1998 World Cup final at the same St Denis stadium outside Paris where the Brazilians had lost 3-0 to the host nation.
BETTER START
Brazil made by far the better start, with Ilan meeting Kleberson's cross to the far post with a powerful header that beat Rustu but slammed against the post.
Rustu had no better luck minutes later, when he flapped at a high ball to the far post and was robbed of possession by Ronaldinho. Adriano collected his square pass but the striker's shot was cleared off the line by skipper Bulent Korkmaz.
There was no escape for Turkey, though, in the 23rd minute when a long punt upfield caught the defence napping and Adriano was able to knock the ball over the advancing Rustu.
Adriano turned potential goalmaker with a sidefoot pass for Kleberson, who fluffed his shot, as Brazil comfortably saw out the half in full control of the match.
That situation took a turn for the worse for the South Americans eight minutes after the re-start, though, when Turkey defender Ibrahim Uzulmez chipped the ball over the Brazil rearguard.
In a copycat finish to Adriano's goal, Karadeniz lobbed the ball over the advancing Dida for his second international goal to put Gunes's men back on course for the semi-finals.
The goal blew the match wide open, with Brazil frantically pushing forward in search of a winner and Turkey looking to use the pace and dribbling of striker Tuncay Sanli on the break.
The next real chance fell to Brazil's overlapping right-back Maurinho, put through by Alex for an angled shot which Rustu blocked.
The cut and thrust was bound to draw blood sooner or later and the triumph went to Turkey when Yilidiray Basturk sprinted through on the break and slipped the ball wide for Yilmaz to score.
Alex's strike in a pulsating finale could only make the scoreline more respectable for Carlos Alberto Parreira's side, who were missing the likes of Ronaldo, Rivaldo, Roberto Carlos and Cafu for the tournament.