Red Bull’s Max Verstappen finished second on Sunday after starting in 18th place.
“Congratulations to Kimi, and great job from Max too. This was the best we could do today, and it was great that we got to do some racing,” said Hamilton who had started on pole position and was looking for a sixth win at the Circuit of the Americas.
Raikkonen, whose last win was at the 2013 Australian Grand Prix, was deadpan.
“Obviously I’m much happier than finishing second. It’s been a great weekend and the car has been pretty good all the time,” said the 39-year-old Finn.
Raikkonen, who will leave Ferrari for Sauber at the end of the season, claimed the lead on the opening lap and rode his luck in a strategic and tactical race to claim the 21st victory of his career.
It was his first win in 113 races, the longest period between wins in F1 history, breaking the previous record held by Italian Riccardo Patrese.
Raikkonen came home 1.3 seconds ahead of Verstappen with Hamilton finishing a further second adrift after failing to battle past the Dutchman in the final laps.
Vettel recovered from an opening lap spin to keep is slender title hopes alive.
Hamilton’s Mercedes teammate Valtteri Bottas was fifth ahead of Nico Hulkenberg and his Renault team-mate Carlos Sainz.
Esteban Ocon of Force India came home eighth ahead of Kevin Magnussen of Haas and Sergio Perez in the second Force India.
On a warm sunlit day, Hamilton made a clean getaway from pole, but was unable to resist Raikkonen’s surge up the hill on the inside line into Turn One.
The Finn claimed the lead, but delight on the Ferrari pit wall was quickly dimmed when Vettel, having attacked and passed Red Bull’s Daniel Ricciardo under braking spun at Turn 13 after the pair had clashed.
Vettel recovered and rejoined in 15th place, but his slim hopes of keeping alive his title challenge appeared reduced to near invisible.
After the chaotic flurry of first lap action, which included several collisions further back in the field, the luckless Ricciardo retired again with an engine failure, parking his car at the start of the back straight.
A Virtual Safety Car was signalled. Unexpectedly, Ferrari did not call in Vettel and it was Hamilton who was the first of the leaders to come in for new tyres on lap 11.
A few rapid laps later, Hamilton passed his team-mate who eased over to him a clear run at Turn One in pursuit of Raikkonen.
By lap 20, Raikkonen was struggling to resist Hamilton who closed within a tenth of a second, but he held him at bay until he pitted on lap 22.
This left Hamilton leading before Verstappen pitted for super-softs, a move that enabled him to jump ahead of Bottas, when the Finn came in a lap later.
This tactical switching then saw Vettel give up second to Raikkonen before he finally pitted on lap 27, re-joining fifth, 43 seconds adrift of Hamilton, but on much fresher tyres.
By lap 33, it was clear Ferrari had the pace to compete and Hamilton was nursing his tyres.
Opting for a cautious strategy, Mercedes then pitted Hamilton again, leaving him with 19 laps to recover after re-joining fourth ahead of fifth-placed Vettel.
Raikkonen led ahead of Verstappen and Bottas.
“Nineteen laps to go now, Lewis, we just need everything,” Mercedes told Hamilton, who regained third on lap 40 when Bottas slowed for him at Turn 12.
Vettel closed up on Bottas with 10 laps remaining as a close finish for the leading four cars looked certain.
Hamilton needed to finish second, with Bottas retaining fourth ahead of Vettel, to secure his fifth title, but in the final laps it was Ferrari’s strategy which triumphed.