The pole position for the United States Grand Prix went to Carlos Sainz, who beat out Charles Leclerc and Max Verstappen.
The world champion will be second on the grid because Leclerc has a 10 place penalty.
Red Bull's Perez has a five-place grid penalty.
Sainz and Verstappen will start ahead of Hamilton.
Verstappen paid tribute to the Red Bull co-owner after he qualified.
Verstappen will equal the all-time record for wins in a season if he wins on Sunday. It has been a difficult day for me because he has done so much for Red Bull.
We are going to try to make him proud tomorrow, even though we tried to give it all in qualification.
The car is a bit stronger in the race than it is in the qualification round. It was very close to the mark today. It would have made a difference if you could change your tires a bit better. We are there and I expect a good race tomorrow.
Sainz said it was enjoyable. Every corner is an adventure with the winds we have today, you don't know how much grip you'll get at each corner.
Red Bull are the favorites. They have a faster pace. We are going to do everything we can to stay in front and win the race.
Mercedes' most competitive performance since the Dutch Grand Prix was achieved by Hamilton, who was third after the first run.
He was ahead of Verstappen after the first runs, but made a mistake at the start of his final lap and dropped down.
Behind him, Mercedes team-mates George Russell and Lance Stroll were both in the top seven.
Russell will be on the second row.
Fernando Alonso will be disappointed with ninth in the Alpine since he has a five-place grid penalty. He uses too many engine parts.
The impressive Alex Albon in the Williams finished in the top 10.