The race restarted in absurd fashion with only Hamilton on the grid, while others were changing their tires because the track had dried. Hamilton had to do the same a lap later and came out in last place, a couple of spots behind Verstappen.
But a clever second tire change by Mercedes for hard tires on Lap 20 outwitted Red Bull and Hamilton moved ahead of Verstappen after he came in for new tires.
Now it was Verstappen's turn to feel frustration as he was stuck in 11th while Hamilton was already up to fifth by Lap 35 of 70.
With 30 laps to go, the top four was Ocon-Vettel-Sainz Jr.-Hamilton.
To give Hamilton a shot at victory, Mercedes made a third tire change with a little more than 20 laps remaining.
But he had to get ahead of the 40-year-old Alonso first, a driver with 32 wins, 97 podiums and among the most talented of his generation.
Alonso held off a first attack on Lap 55 and blocked him two more times on Lap 57 with stellar defending on the inside.
Alonso's nous was enough to repel two more attacks as Hamilton was blocked again on the outside and they nearly collided.
When Hamilton finally got past him there were only five laps left and Sainz Jr just ahead. He overtook him but by then the front two were too far ahead.
Hamilton's bid for a 100th win resumes on Aug. 29 at the Belgian GP in Spa.
Bottas will start that race with a five-place grid penalty for causing the accident.
Alonso's brilliant defensive driving helped Ocon secure the victory and they hugged after the race, with Alonso lifting him up in the air.
Hamilton was again loudly jeered by sections of the crowd, as he was after taking pole position on Saturday. He won the British GP two weeks ago despite a 10-second penalty for taking Verstappen out on the first lap.
Heavy rain fell shortly before Sunday's race and the track was soaked. Hamilton got away from pole but Verstappen shot ahead of Bottas into second.
McLaren's Lando Norris also overtook Bottas, who bumped into the back of him— sending Norris sliding into Verstappen and both off track. Bottas went into a tire wall and took out Verstappen's teammate Sergio Perez on his way off the track.
Just behind them, Ferrari's Charles Leclerc was jammed between Aston Martin's Lance Stroll and McLaren's Daniel Ricciardo.
A red flag halted the race to allow stewards to clear the wrecked cars. Leclerc, Stroll, Norris and Perez retired.
When the race restarted about 30 minutes later, Hamilton was alone on the grid while others were ditching their wet-weather tires to suit the now drying track.
Hamilton had to do the same on the next lap and came out in last place with Verstappen 11th, while Ocon led from Vettel. Nikita Mazepin was the sixth to retire after colliding with 2007 F1 champion Kimi Raikkonen's Alfa Romeo in the pit lane.
The sinewy and tight 4.4-kilometer (2.7-mile) Hungaroring circuit is arguably the hardest track to overtake on after Monaco.
“I can't get close to people,” a frustrated Hamilton said on the radio.
The tension also affected Verstappen. In his haste to overtake Mick Schumacher, he went wide and then bumped tires with him moments later.
Hamilton got past Schumacher on Lap 16 but found AlphaTauri's Pierre Gasly difficult to catch.
But he was soon in his usual overtaking mode.
“Lewis, you can win this,” Mercedes team principal Toto Wolff said after the third tire change.
But getting past Alonso is a different prospect to most drivers, and Hamilton fell short of winning the Hungarian GP for a record-extending ninth time.
The day belonged to Ocon, who was once part of the Mercedes development program and the team's reserve driver two years ago after losing his F1 seat.
He showed the talent Mercedes saw in him with a composed drive under pressure from Vettel, a four-time F1 champion with 53 wins.
“Esteban didn't make a single mistake,” Vettel said. (AP)