Hamilton stunned Bottas to snatch pole position for F1's Portuguese GP, after the Finn had topped every practice session and Q3.
Max Verstappen qualified third for Red Bull, ahead of Ferrari's Charles Leclerc.
At the end of the qualifying session at Portimao, which was delayed by 30 minutes so a drain cover at Turn 14 that had been dislodged at the end of FP3 was repaired, the Mercedes drivers made the unusual decision to go for their final runs on the medium tyres.
Bottas led the way on the 1m16.986s that he had set on the softs, with Hamilton needing to find 0.087s.
The world champion then left his garage with over four minutes of the session remaining on the harder rubber, which he used to take first place with a 1m16.934s.
Bottas wrestled back the top spot with a 1m16.754s, with Verstappen, still on the softs, then getting ahead of Hamilton.
But the world champion had enough time and fuel to go for a second run, which he used to leap from third to pole with a 1m16.652s.
The two Mercedes drivers will also start on the race on the medium tyres, handing them a likely strategy advantage over Verstappen.
Leclerc, who also came through Q2 on the mediums, took fourth with a late improvement on his final lap, ending up ahead of Sergio Perez, and the pair of them shuffling Alex Albon down to sixth in the other Red Bull.
The McLarens of Carlos Sainz and Lando Norris took seventh and eighth, with Pierre Gasly ninth for AlphaTauri.
Gasly faces a post-qualifying investigation alongside Perez regarding an issue in Q3 at Turn13.
Daniel Ricciardo had only just squeaked into Q3 after he spun off going through the high-speed right of Turn 11 as he passed one of the Mercedes cars and his teammate Esteban Ocon.
The Australian driver slid backwards through the gravel and touched the wall with the rear of his Renault, but was quickly able to drive away.
However, Renault could not fix the necessary repairs in time for Ricciardo to set a time in Q3 and he will start 10th as a result.
Ocon had already failed to improve on his final lap and was knocked out in 11th as a result, ahead of Lance Stroll and Daniil Kvyat.
George Russell, who had nipped through to Q2 for the seventh time in 2020 with a last-gasp improvement in Q1, beat Sebastian Vettel to P14.
Vettel was also lapping on the medium tyres throughout Q2, but a lock-up at Turn 3 and feeling his tyres "were not ready" on the new, low-grip surface at Portimao cost the Ferrari driver.
In Q1, which was delayed for 30 minutes by the track repair work, Russell improved with the last timed lap of the opening segment, setting personal bests in all three sectors, as he therefore knocked Kimi Raikkonen out in 16th.
Behind Raikkonen came his teammate Antonio Giovinazzi, while Romain Grosjean was shuffled down from P15 to P18 in the closing moments of Q1 as he could not improve – and not in any individual sector, either – on his final lap.
Kevin Magnussen finished P19, while Nicholas Latifi brought up the rear of the field in 20th, as he too failed to go faster at any stage on his final lap.
|3||Max Verstappen||Red Bull||1'16.904||0.252|
|5||Sergio Perez||Racing Point||1'17.223||0.571|
|6||Alex Albon||Red Bull||1'17.437||0.785|
|7||Carlos Sainz Jr.||McLaren||1'17.520||0.868|
|12||Lance Stroll||Racing Point||1'17.626||0.974|
|16||Kimi Raikkonen||Alfa Romeo||1'18.201||1.549|
|17||Antonio Giovinazzi||Alfa Romeo||1'18.323||1.671|
|View full results|