Hamilton, often on the back foot in qualifying, took pole by 0.086 seconds.
But the story of the day was in many ways Ferrari’s strategic error that saw Charles Leclerc knocked out in the first session.
Ferrari elected not to send Leclerc out for a second run, and the Monegasque will start 15th for his home race.
Red Bull’s Max Verstappen was third, ahead of Ferrari’s Sebastian Vettel, who was the man who knocked out Leclerc.
What were Ferrari doing?
Vettel was struggling after a poor first run in first qualifying and needed to go out again to get past the first session.
But Ferrari did not see the danger facing Leclerc, despite the fact that his time at that stage was less than 0.2secs quicker than Vettel’s.
Leclerc was delayed when he missed the weigh bridge and had to be pushed back by his mechanics for the car to be checked, but he said that had not been the cause of the problem.
A distraught Leclerc, who grew up in a flat just next to the main straight of the Monaco track, said: “I asked whether they were sure. They said we think we are. I said, ‘Shouldn’t we go again?’ I didn’t have any explanations yet in detail.
“Very difficult one to take – we had plenty of time to go out again. The weigh bridge was not the problem. We had the fuel to go again, we just needed to put some tyres on. I need some explanations.”
Ferrari are yet to say what happened.
Vettel, struggling all day after crashing in final practice, was 0.781secs off the pace, after hitting the wall at Tabac on his final run.
The Mercedes battle
The Mercedes, sporting a red halo head-protection device in tribute to the late three-time champion Niki Lauda, their non-executive chairman, who died on Monday, were in a private battle for pole.
Bottas had looked quick all weekend and the Finn was more than 0.2secs up on his team-mate after the first runs in the final session but when it mattered Hamilton managed to pull a lap out of the bag, while Bottas failed to improve on his final run.
Hamilton said: “I love it here. It’s my home. this is the race every driver dreams of. We’ve arrived with a great car, a great battle with Valtteri. He has been quick all weekend. The desire and the will to get this pole, it means so much to me.
“I’m really grateful to my team. The lap was just beautiful.”
“The lap was hard. All of us drivers take our car, fast or slow, to the limit. And when you’re at the limit, it’s like wrestling a bull. I had an oversteer moment in the Rascasse, and I just held on. I don’t know what the gap is, but I’ll take it – it’s perfect.”
It was Hamilton’s 85th pole position of his career and the reigning world champion added: “This is definitely one of the best poles I can remember.
“We have had lots of success over the years but I can’t remember one that means as much as this one. It has been such a difficult week for the team. We have had a cloud over us all weekend and we have been trying to lift each other up.”
Bottas said he had been affected by traffic preparing for his final run.
“I’m disappointed. The first lap, there was still plenty of places I could improve, on the out lap I had a lot of traffic and I couldn’t get the tyres hot enough for the fast lap. I thought I had it today but it wasn’t to be.”
A tribute to Lauda
Hamilton was given an exemption from media duties on Wednesday in the wake of Lauda’s death, a decision that has received some criticism, and after qualifying he explained his feelings on the matter.
“The other day I didn’t feel I was ready,” he said. “It wasn’t the time to dig into our feelings.
“I was really in touch with Niki a lot through this past eight months. We’d be sending videos back and forth to each other. It was always difficult because sometimes he looked good and was perky and said he would see us at this or that race, and others days when he didn’t.
“Ive always talked about how Ross [Brawn, former team boss] was the convincing element of me coming to the team but Niki was the one who phoned me up and said: ‘Mercedes is where you should be.’ I had never really spoken to him before that. He brought it to me and really got it across the line.
“All these years he has been my partner in crime. He was such a racer. He was part of the process of changing my life.
“If I hadn’t had that call, I’d be a one-time champion now. I sit here as a five-time champion and I owe him a lot. It was really difficult earlier in the week and I don’t feel I have to confirm to what everyone else thinks. Wednesday was not the time. But he will live on in all our memories.”
Behind Vettel, Red Bull’s Pierre Gasly took fifth but will drop three places for Sunday’s race after being penalised for impeding Haas driver Romain Grosjean.
Magnussen qualified sixth, ahead of Renault’s Daniel Ricciardo, Toro Rosso’s Daniil Kvyat, McLaren’s Carlos Sainz and the second Toro Rosso of Anglo-Thai rookie Alexander Albon.
British novice Lando Norris was 12th in the second McLaren, while George Russell kept up his clean sweep of Williams team-mate Robert Kubica in 19th place.