The story begins in the immediate aftermath of the successful Mongolian conquest of Bagdad by Hulagu Khan (Kurt Katch). The caliph Hassan (Moroni Olsen) has escaped captivity, together with his young son Ali (Scotty Beckett), and prepares to regroup the renmants of his troops. While staying at the mansion of Prince Cassim (Frank Puglia), Ali and Cassim's daughter Amara (Yvette Duguay), fearing that they will not see each other again, betroth themselves via blood-bond.
As the caliph prepares to leave, Cassim stops him at the last moment. This, however, is the initiation for an ambush by the Mongols, to whom the cowardly prince has sworn allegiance; the caliph and his retinue are massacred, and only Ali escapes. Alone and lost in the desert, he comes across a mountainside where he sees a group of riders exiting a hidden cave. Deducing its opening phrase, he enters the cave and finds it filled with treasure. When the 40 thieves return, they find the boy asleep in their hideout. Upon learning that he is the son of the caliph, and impressed by his courage and determination, the thieves allow him to stay, and their leader, Old Baba (Fortunio Bonanova), adopts him as his son, Ali Baba.
Ten years later, the band of thieves have become a group of Robin Hood-style resistance fighters, raiding the Mongols and giving to their poor and downtrodden people. One day, they learn of a caravan bearing the new bride for the Khan to Bagdad, which seems to be rich pickings because it is apparently only loosely guarded. However, Ali Baba, now a grown man (Jon Hall), is suspicious and decides to scout the caravan first, along with his 'nanny' Abdullah (Andy Devine). The bride turns out to be Amara (Maria Montez), Cassim's daughter, who is to be wed to the Khan in order to solidify Cassim's somewhat shaky standing with the Mongols.
In the meantime, Amara decides to take a bath in the oasis, where Ali encounters her (they do not recognize each other, however). Taking her for a mere servant girl and passing himself off as a traveller, he asks her about the caravan, then more about herself. But then it turns out that the caravan is in fact heavily guarded; Ali is ambushed and captured, while Abdullah narrowly escapes. Upon learning that the 'servant girl' is the bride of the Khan (her name is not mentioned), Ali curses her for her supposed treachery. Hurt by his words and in growing admiration for him and his cause, she asks her servant and bodyguard, Jamiel (Turhan Bey), who hero-worships the 40 thieves, to give Ali some water for the trip.
In Bagdad, Ali is presented to the Khan, though he is not recognized as the leader of the 40 Thieves, and bound to a pillory in the palace square for public execution the next day. Cassim visits him in private and discovers Ali's true identity, but keeps the knowledge to himself. Soon afterwards, the thieves mount a rescue, but Old Baba is mortally wounded; Amara, who went to see Ali to clear the misunderstanding between them, is kidnapped, and Jamiel personally cuts Ali loose from his bonds. The thieves retreat into Mount Sesame.
The next day, the thieves capture Jamiel, who was tracking them. Ali recognizes him as a friend, and Jamiel, who swears allegiance to Ali Baba, is assigned as a spy in the palace. His first task is to deliver a ransom note to the Khan: in exchange for his bride, Hulagu Khan is to surrender the traitor Cassim. The thieves proceed to Cassim's mansion to await the traitor's arrival. When Amara walks into the garden, Ali recognizes her as his lost love, and with his re-awakened feelings for her he decides to release her without waiting for her father. This initially arouses the anger of his band, but they still remain loyal to him.
When Amara returns to Bagdad, her father confesses Ali's true identity to her and the Khan. Hulagu Khan decides to hold the wedding immediately; Amara refuses, but the sight of her father being tortured (actually, a ruse) forces her to give in. Jamiel brings the news to Ali, who decides to free his love. In order to reach the palace unnoticed, he devises the plan to pose as a merchant from Basra who brings forty huge jars of oil as a wedding gift. Jamiel returns to the palace to relay the plan to Amara, but they catch one of her servants eavesdropping. The girl then relays the news to Cassim and the Khan, who decide to welcome Ali in a fitting manner.
At the wedding day, Ali does appear as the merchant and is admitted as a guest. During an interlude, sword dancers appear, who first perform their routine and then suddenly plunge their weapons through the jar covers - but the jars contain only sand. Upon discovering the exposure of the original plan, Ali had decided to make a few changes: most of the thieves came disguised in the crowd; some others were hidden in jars which were not brought before the Khan.
Hulagu Khan kills Cassim for his failure and announces Ali's execution, but then Jamiel opens the revolt by dispatching Ali's guards with his throwing knives. While the thieves attack the palace guards, he and Amara open the gates for the mob, which storms in and overpowers the Mongols. Hulagu Khan is killed by Abdullah while preparing to finish Ali, and as a sign of victory Jamiel hoists the Arabian flag atop the palace's highest tower.Maria Montez as Amara
Jon Hall as Ali Baba
Turhan Bey as Jamiel
Andy Devine as Abdullah
Kurt Katch as Hulagu Khan
Frank Puglia as Prince Cassim
Fortunio Bonanova as Old Baba
Moroni Olsen as Caliph Hassan
Ramsay Ames as Nalu
Chris-Pin Martin as Fat Thief
Scotty Beckett as Ali Baba as a Child
Yvette Duguay as Amara as a Girl
Noel Cravat as Mongol Captain
Jimmy Conlin as Little Thief
Harry Cording as Mahmoud
Ed Agresti as Mongol Captain (uncredited)
Richard Alexander as Mongol Guard (uncredited)
Jerome Andrews as Dancer (uncredited)
Robert Barron as Mongol Captain (uncredited)
Alphonse Bergé as Tailor (uncredited)
Eric Braunsteiner as Dancer (uncredited)
Ed Brown as Dancer (uncredited)
John Calvert as Thief (uncredited)
Fred Cavens as Thief (uncredited)
Dick D'Arcy as Dancer (uncredited)
William 'Wee Willie' Davis as Arab Giant (uncredited)
Dick Dickinson as Thief (uncredited)
Rex Evans as Arab Major Domo (uncredited)
Martin Faust as Thief (uncredited)
Alex Goudavich as Dancer (uncredited)
Hans Herbert as Thief (uncredited)
David Heywood as Thief (uncredited)
James Khan as Persian Prince (uncredited)
Ethan Laidlaw as Thief (uncredited)
Pierce Lyden as Guard (uncredited)
George Martin as Dancer (uncredited)
Don McGill as Guard (uncredited)
Art Miles as Mongol Guard (uncredited)
Belle Mitchell as Nursemaid (uncredited)
Alma M. Pappas as Princess Kanza Omar (uncredited)
Theodore Patay as Arab Priest (uncredited)
Joey Ray as Thief (uncredited)
Pedro Regas as Thief (uncredited)
Alex Romero as Dancer (uncredited)
Angelo Rossitto as Arab Dwarf (uncredited)
Charles Wagenheim as Barber (uncredited)
Norman Willis as Guard (uncredited)
Harry Woods as Mongol Guard (uncredited)
The role of Jamiel was meant to be played by Sabu. However, when he went into the army, the role was taken by Turhan Bey.
Maria Montez admitted she only acted "three or four times" in the film.