After the Mercedes drivers were put on different tyre strategies, Bottas moved out of the way for Hamilton on two occasions during the 57 lap race, but on the second occasion he effectively ceded second place to his teammate.
Despite twice being let through by team-mate Valtteri Bottas, Hamilton was unable to chase down Vettel and had to settle for second, finishing 6.6 seconds behind the Ferrari driver. To try to minimise the delay he slowed on entry backing up the Red Bull of Daniel Ricciardo.
Pole-sitter Valtteri Bottas finished third in his Mercedes, on a day when the Finn struggled for race pace and team-mate Hamilton thrived. Max Verstappen made a brilliant start from sixth to get ahead of teammate Ricciardo in fifth while Raikkonen dropped from fifth to seventh behind Massa.
Who is top of the F1 Drivers' World Championship after Round 3?
"It was crucial for us to get between them to not allow them to get in front and pull away and do their thing - upset them a bit", Vettel after the race.
Verstappen asked his team to match that strategy and he came in on the next lap.
Verstappen was helpless as he watched his auto go straight on at turn 4 and into the barriers at low speed and out of the race.
"Yes guys, that's what I'm talking about", whooped the German, who was helped by a safety auto after an aggressive early pitstop. Mercedes took the opportunity to pit Bottas and Hamilton from first and second, but in doing so was forced to double stack its cars in the pit lane.
Both Mercedes drivers used the safety vehicle incident to pit for new tires, but Hamilton held up Ricciardo. Exclusively my fault. Then you get the time lost in the pit lane.
Near the end of the Bahrain GP, Hamilton had good pace on Vettel, but the gap was just too wide and there was the penalty to consider as well. Mercedes had fitted his auto with soft tyres while Vettel and Bottas had gone for super-softs, which in theory meant Hamilton could attempt to go to the end of the race while the lead two still had to stop.
The three-time world champion said the penalty was a setback, but that his Mercedes may not have been fast enough to overhaul the triumphant Ferrari of Sebastian Vettel.
"It is the small percentages now which is what racing should all be about", Hamilton added.
Kimi Räikkönen came home 4th in the other Ferrari.
Hamilton has won 54 races while Bottas, who was previously at Williams, has yet to triumph. Vettel pitted three laps later and dropped to third behind Raikkonen, who had recovered from his slow start but was still yet to make a second stop. Vettel was braver on the brakes and passed Hamilton around the outside. Despite pitting later to serve his penalty and switching to a fresh set of soft tyres, it only helped him get back his second place but not take the win.
Bottas kept the lead for the opening laps, with Vettel close behind in a five-car train.
"But finally, it was a controlled race". It was a valiant effort by Hamilton after his error in the pit lane, but ultimately Ferrari had the pace to beat Mercedes and the mistakes made under the safety vehicle proved costly for the world champions.
Vettel's 44th win lifted him to 68 points, with Hamilton second on 61 and the championship looking ever more likely to be an evenly matched battle between the two with Ferrari and Mercedes a cut above the rest.