Hamilton posted a decisive 1 minute 40.593 seconds around the 6.003-kilometre street course in Baku in a final qualifying interrupted by red flags when Australia's Daniel Ricciardo clipped a wall with his Red Bull.
Then the red flag was brought out with three and a half minutes to go as Daniel Ricciardo parked his Red Bull on one of the straights after hitting the rear of the vehicle on the wall.
Force India pair Sergio Perez and Esteban Ocon finished sixth and seventh quickest respectively while Williams' Lance Stroll continued his improvement following a points finish in Canada last time out, as he qualified eighth fastest, narrowly edging out team-mate Felipe Massa. "Still not ideal, and I think that if you can switch them on, you can go a lot faster".
When teams turned their attention to qualifying simulations on the supersoft tyres, Hamilton and Bottas traded fastest laps.More news: Geely signs final contract with DRB-Hicom
"The team really did a fantastic job to set the auto up in the right way and Lewis with his lap here - I have never seen anything like it", said the 68-year-old Austrian.
"After the red flag, there was a lot of pressure on that final lap". We've been struggling to get temperature into these tires, we don't know if it's a one-lap tire, so it was all or nothing, but the lap just got better and better throughout. "We need to get on top of that because it shouldn't happen".
"The first lap I had the time but I made a mistake", he added.
"I think we, as a team, probably prefer not to talk so much and just get on with it".More news: How will Amazon transform Whole Foods? (The 3:59, Ep. 242)
Sebastian Vettel: "The result is okay, but obviously I am not entirely happy and this qualifying didn't help. Today was a good recovery and I thought I will not allow myself to do that again next qualifying but you learn from these things".
Romain Grosjean and Marcus Ericsson also were eliminated in the first part of qualifying, with the Haas driver struggling with brake problems throughout the weekend and being eliminated in 17th ahead of the Sauber.
Alonso will start in an embarrassing 16th and Vandoorne next to last in 19th.
Jolyon Palmer was unable to take part after his Renault caught fire due to a fuel leak during the morning final practice, but approached the stewards for permission to take part in Sunday's race.
The Toro Rosso cars claimed row six - but Carlos Sainz Jr. will drop three-places on the grid thanks to his Canadian GP penalty - ahead of Haas' Kevin Magnussen and Renault's Nico Hulkenberg.More news: Amazon To Acquire Whole Foods For $13.7 Billion