Ferrari's Felipe Massa will start on pole in Sunday's Turkish Grand Prix ahead of McLaren's Lewis Hamilton.
Massa has claimed pole in Turkey for the second successive year
The Brazilian edged a tight qualifying session ahead of Briton Hamilton, with team-mate Kimi Raikkonen in third and McLaren's Fernando Alonso in fourth.
Massa had appeared out of the running but set a lap of one minute, 27.329 seconds in the dying moments for the eighth pole position of his career.
The time denied Hamilton, who had just set a lap of 1:27.373.
The Turkish Grand Prix is the first race since Hungary, which saw Hamilton and Alonso publicly fall out after a controversial qualifying session.
The two drivers claim to have patched up their differences during the three-week break and they had their own pits boxes and crews in Istanbul to avoid any conflict.
I put in a great lap and I am very proud
"It was easier to have two pit crews, and I think it worked quite well," said Hamilton.
The 22-year-old leads the championship by seven points from Alonso and was happy was his performance, saying: "I'm quite pleased with that.
"I kept on pushing, losing a bit of time in the last corner, which is a tricky corner. But it's good for us and good for the team.
"The car is good. The team have been pushing to gain another step. We've a good package, which stands us in good stead to have a fight with the Ferraris.
"Felipe got me there in the end, but I gave it everything. It was a big push and I really had to hang it out. It just wasn't quite enough, but it was a good session for me."
It was a much-improved display from Massa, who could only qualify 14th in Hungary after a series of problems.
"It was very tough, very tight," said the 26-year-old Brazilian, who won in Turkey last year. "It was a big fight between all four drivers.
"But I put in a great lap and I am very proud, especially after such a bad result in Hungary. We deserved it."
Spaniard Alonso switched to hard tyres for his final attempt during the top-10 shoot out but he struggled for pace and had to settle for fourth.
BMW Sauber drivers Robert Kubica and Nick Heidfeld were fifth and sixth respectively.
Briton Anthony Davidson qualified in 11th after narrowly missing out on a top-10 spot for the first time this season.
The Super Aguri driver will start from 11th, equalling his best position since the season-opening race in Australia.
David Coulthard was 13th, Jenson Button 15th, while Ralf Schumacher's miserable season continued as he failed to make it out of the first session.
The German Toyota driver will start Sunday's race in 18th position.
Final qualifying result:
1. Felipe Massa (Brz) Ferrari 1 min 27.329 secs
2. Lewis Hamilton (GB) McLaren 1:27.373
3. Kimi Raikkonen (Fin) Ferrari 1:27.546
4. Fernando Alonso (Sp) McLaren 1:27.574
5. Robert Kubica (Pol) BMW Sauber 1:27.722
6. Nick Heidfeld (Ger) BMW Sauber 1:28.037
7. Heikki Kovalainen (Fin) Renault 1:28.491
8. Nico Rosberg (Ger) Williams - Toyota 1:28.501
9. Jarno Trulli (It) Toyota 1:28.740
10. Giancarlo Fisichella (It) Renault 1:29.322
11. Anthony Davidson (GB) Super Aguri - Honda 1:28.002
12. Mark Webber (Aus) Red Bull - Renault 1:28.013
13. David Coulthard (GB) Red Bull - Renault 1:28.100
14. Rubens Barrichello (Brz) Honda 1:28.188
15. Jenson Button (GB) Honda 1:28.220
16. Alexander Wurz (Aut) Williams - Toyota 1:28.390
17. Vitantonio Liuzzi (It) Toro Rosso - Ferrari 1:28.798
18. Ralf Schumacher (Ger) Toyota 1:28.809
19. Takuma Sato (Jpn) Super Aguri - Honda 1:28.953
20. Sebastian Vettel (Ger) Toro Rosso - Ferrari 1:29.408
21. Adrian Sutil (Ger) Spyker - Ferrari 1:29.861
22. Sakon Yamamoto (Jpn) Spyker - Ferrari 1:31.479