Hamilton happy overnight work paid off for Mercedes
The seven-time champion, who had feared a struggle to make the top ten after Friday's practice sessions, said he had felt he was a man on a mission when he woke up in the morning.
"I'm absolutely shocked," he said. "It was a real surprise to see us so far up at the front and to see us competing for, ultimately, the front row.
"But we did some great work overnight with the analysis and I did a lot of work to try and figure out where we can find the time.
"I knew when I woke up this morning that I was on a mission, but I didn't expect us to be fighting for the front row.
"The car really came alive today. A big thank-you to everyone back at the factory because it does show that we are progressing. Today, I really enjoyed driving the car."
Hamilton, who is out of contract at the end of this year, but is involved in talks to prolong his stay with the team, had been fastest ahead of Max Verstappen's Red Bull in the early stages of an unpredictable session and survived a minor collision with team-mate George Russell in the second session.
Russell failed to progress to the top ten shootout and qualified 12th while Verstappen took his maiden Spanish pole.
Asked what had gone wrong for him, Russell said: "Everything. I'm not sure why… Straight out of the box, in run one, I just had no grip in the tyres.
"I was really struggling and the car started to bounce a lot in the high-speed corners, so I couldn't take the corner flat-out when we were doing so this morning."
Of his incident, when he pulled across Hamilton as he accelerated to pass him, he admitted he did not see him.
"I wasn't even aware he was there," he said. "I started my lap, trying to take a slipstream from (Carlos) Sainz and fortunately nothing bad happened."