Lewis Hamilton will start the US Grand Prix on pole position as he seeks to wrap up a fifth world title.
The Mercedes driver beat title rival Sebastian Vettel by 0.061 seconds but the German will be demoted to fifth on the grid because of a penalty.
Hamilton will clinch the title on Sunday if he wins the race and Vettel finishes lower than second.
Potentially crucially for Vettel, his team-mate Kimi Raikkonen starts second, ahead of the second Mercedes of Valtteri Bottas.
Vettel will also be behind the Red Bull of Daniel Ricciardo, who qualified fifth but will be promoted to fourth by the German’s penalty, which was given for failing to slow down sufficiently under red flags during practice.
Ferrari back on form a little too late
Hamilton had to pull out all the stops to seal pole on a track where he has won five of the six races held after Raikkonen beat the time the Briton set on his first lap.
It was an unexpected return to form from Ferrari, who had been closely matched with Mercedes for most of the season but had dramatically fallen away in the last three races.
Hamilton said: “That was very important. That was close. I didn’t know how close it was going to be but by the last run I knew it was quite edgy.
“I knew I had to do solid laps. The first lap was decent but not good enough and the second one was just that bit better. The Ferraris were really quick.”
The top three were all covered by less than 0.1secs.
Vettel surprised by Ferrari speed
Vettel said: “It was pretty close. Always a bit of a shame when you miss out on just that little time – 0.06secs you can always say you had them in there somewhere but I was happy with the laps I had.
“Today has been surprisingly close. I don’t think we expected to be that close.”
Vettel, who has had to fight back through the field a number of times this year after various incidents, employed some black humour when asked about his grid penalty and the need to make up ground if he wanted to keep the title alive beyond this weekend.
“I am used to it,” he said. “I know how to do it now.”
Only two weeks ago in Japan, he started eighth after errors by team and driver in qualifying, but crashed with Red Bull driver Max Verstappen as he attempted to fight up through the field, eventually finishing sixth.
This time, Vettel will not have to worry about Verstappen. The Dutchman qualified 15th because his car suffered a loss of drive at the end of the first qualifying session.
He will likely start 13th because of grid penalties for both Toro Rosso drivers, who qualified ahead of him.
Looking at the race
In theory, Hamilton should be able to control the race from the front, but there will be a concern at Mercedes about Raikkonen.
The Finn will start the race on the gripper ultra-soft tyres, while Hamilton, Bottas, Ricciardo and Vettel will be on the super-softs. It remains to be seen whether that gives the Finn a strategic advantage.
However, Raikkonen has not made up a place on the first lap of a race since the 2016 Abu Dhabi Grand Prix.
Mercedes will be trying to orchestrate the race so that Bottas stays ahead of Vettel, who needs to ensure Hamilton does not score eight more points than him to keep the title alive to the race in Mexico next weekend.
And the rest
Behind the big three teams, Esteban Ocon qualified his Force India best of the rest, on a weekend when it emerged the Frenchman is in the frame for a seat at Williams next year after it had looked as if he might end up sitting out 2019.
Ocon beat Renault’s Nico Hulkenberg, Romain Grosjean’s Haas and Sauber’s Charles Leclerc.
McLaren’s negative spiral continued as Fernando Alonso was knocked out in the first session and qualified 16th. His team-mate Stoffel Vandoorne, who has been dropped for 2019 and will race in the all-electric Formula E series, was slowest of all.