A remarkable race that featured three safety cars and several crashes, including clashes between team-mates, was won by Red Bull's Daniel Ricciardo.
Vettel was given a 10-second stop-go penalty for swerving into Hamilton's Mercedes as they prepared for a restart at the end of one of three safety car periods in Baku.
But the time Hamilton lost being forced to pit for a new head restraint put him behind Vettel. He followed him past a number of cars as they recovered positions, and closed up as the race entered its final lap, but the Englishman was not able to pass.
Hamilton ended up finishing fifth, a place behind Ferrari's Vettel, and lost two points to the German. He is 14 points behind after eight of 20 races.
Hamilton's Mercedes team-mate Valtteri Bottas, who was last and lapped after the first lap, passed Williams driver Lance Stroll for second on the final straight as the 18-year-old Canadian scored his first podium in his eighth race after a mature drive.
The controversial incident between Hamilton and Vettel happened as they prepared for the restart after the second safety car period.
Vettel ran into the back of Hamilton as he accelerated out of Turn 15, the penultimate corner, damaging his front wing. The German then pulled alongside Hamilton's Mercedes and drove into it, banging wheels.
Vettel told his team over the radio that he believed Hamilton had deliberately slowed, saying: "He brake-tested me. What the hell is going on?"
Hamilton told his Mercedes engineers: "Vettel literally just came alongside me and hit me."
When he was told of the penalty, Vettel said: "Tell me when I did dangerous driving." Ferrari told him they would discuss it after the race.
Hamilton said over the radio, addressing his remarks directly at race director Charlie Whiting, that he believed a 10-second penalty was "not enough for driving behaviour like that - you know that, Charlie."
Before the stewards delivered their verdict on Vettel's driving, Hamilton's race had already fallen apart.
He had controlled it from the start, despite the chaos behind him, and was leading Vettel by 2.5 seconds after 28 of 51 laps when the cockpit head restraint padding that protects the drivers from impacts began to lift on the straight.
His race engineer asked him to try to push it back down again but he was unable to force it into position and Mercedes were ordered to pit Hamilton to fix a restraint into position.
He rejoined in eighth place and fought his way up past Esteban Ocon's Force India, McLaren's Fernando Alonso and Haas driver Kevin Magnussen before the end.
Ricciardo was handed victory by the problems of Hamilton and Vettel but he also had to earn it.
He dropped to 17th early in the race with an unscheduled pit stop to clear debris from his brake ducts - but fought his way back up to 10th by the time of the first safety car period.
The chaos of the next two promoted him to fifth, thanks to some aggressive overtaking by the Australian, between Stroll and the other Williams of Felipe Massa, then he passed Stroll into Turn One at the final restart as Massa dropped out with a broken damper.
Stroll hung on for a few laps but Ricciardo eventually edged away to win by four seconds as Stroll was caught by Bottas on the final straight after a superb recovery by the Finn.
Bottas had been lapped at the end of the first lap following a collision between himself and Ferrari's Kimi Raikkonen at the second corner, Bottas bouncing over the kerb and into his fellow Finn, damaging both cars.
But the first safety car period allowed him to un-lap himself and he drove well to pick his way through the field after that.
The first safety car period happened because of a mundane reason - Daniil Kvyat's Toro Rosso had broken down and stopped in a dangerous place and needed to be recovered.
But it set up a domino effect at the restarts.
At the first attempt to restart on lap 17, Vettel was challenged for second by Force India's Sergio Perez, and behind them the second Force India of Esteban Ocon and Raikkonen were side by side with the Finn losing some bodywork.
That and a number of other brushes between cars left debris on the track and the race was immediately stopped for it to be cleared.
The safety car was out for another two laps, but just before the second restart Hamilton and Vettel clashed.
Then, as the race restarted, the Force India drivers collided as they tried to accelerate out of Turn two side by side, creating a whole load more debris, and the safety car was immediately deployed again.
The incident is likely to cause further recriminations at the team, where there was a dispute about team orders at the previous race in Canada.
Two laps later, Alonso went on to the radio to say the race should be red-flagged because there was so much debris. "This circuit is too fast to have this risk," he said.
Shortly afterwards, the red flag was thrown and the race stopped for 15 minutes.
At the restart Nico Hulkenberg looked set for a strong finish in the Renault, running sixth and threatening Magnussen, but he made a critical error in misjudging Turn Seven, breaking his right front wheel on the inside wall.
Ocon recovered from the clash with Perez to take a solid sixth ahead of Magnussen, Toro Rosso's Carlos Sainz and Alonso, who scored McLaren-Honda's first points of the season despite losing battery power in the closing laps.
Ricciardo's team-mate Max Verstappen retired early from fourth place with an engine failure.
Sebastian Vettel was penalised for driving into Lewis Hamilton in a chaotic and incident-packed Azerbaijan Grand Prix but the German still extended his title lead as a loose head restraint cost the Briton victory.