It was also a superb performance from Stroll, the son of a billionaire, who faced heavy criticism earlier in the season. Hamilton accused Vettel of "disgracing himself", while Niki Lauda, non-executive chairman of Hamilton's Mercedes team, said Vettel was "crazy". "Just lost out to Valtteri in the end, but still extremely happy with the result. I started to feel the rear going away so I knew it was going to be tricky to keep the pace".
His Silver Arrows team-mate Valtteri Bottas completed a front-row lockout, with Ferrari's Kimi Raikkonen third fastest in qualifying.
Vettel went on to finish fourth and Hamilton fifth, and they are now separated by 14 points in the title race.
Making the most of a series of extraordinary incidents and interventions in an action-packed race, which included three safety auto interventions and a red-flagged stoppage, Ricciardo, who started 10th, roared home 3.904 seconds ahead of the rest to deliver his first win of the season.
Shortly before that, Hamilton and Vettel were involved in an incident that threatens to sour their good relations.
After the race, Hamilton expressed his frustration as he accused Vettel of risky driving while the German defended himself and claimed the Brit had deliberately slowed down before the pair first made contact following a corner.
The German claims that as Hamilton led the field through Turn 15 he deliberately slowed causing the Ferrari to run into the back of the Mercedes.
Racing resumed, for a third time, on lap 19, but only after the incident in which Vettel hit the rear of Hamilton's vehicle, under braking as the leader tried to avoid the slow-moving safety auto.
Vettel was handed a 10-second penalty for risky driving, but Lauda said the four-man panel of stewards had been too lenient.
"I don't listen to that stuff, it's just people talking", Stroll said defiantly Sunday, when reminded of accusations that his father's financial backing of the Williams team got Stroll the job.
In all of this, Hamilton and Vettel retained the top two spots. "The rest is all just noise in the background and I don't really care for it".
Within moments, the safety auto was out again for the third time after Sergio Perez and his Force India teammate Esteban Ocon collided.
Ten teams, two drivers in each, will compete on the 6-kilometer Baku City Circuit.
With debris littering the track, it was deemed too unsafe to race and the red flag came out on lap 22 of 51. However, with this win, he has moved to fourth in the standings.
Then, the race order changed again as Vettel came in for his time penalty moments after Hamilton changed his headrest. Dropping back to 17th at one stage, Ricciardo worked his way up the order during three Safety Car periods and found himself running in the top five.
Amongst all the chaos, Ocon took sixth, Magnussen in the Haas seventh, Sainz eighth for Toro Ross, a gifted ninth after the carnage for Alonso's McLaren, and Wehrlein picked up the last points place for Sauber.