Set against the backdrop of the fictional Afghan kingdom of "Baharistan", which is ruled by a just and kind Sultan Ali Rizwan Mohhamad Dokeer Khan (Shammi Kapoor) this is a tale of the masked legend, Ajooba. All is well in the land except that the Sultan seemingly can't have children.
An evil devil-worshipping Vazir (Amrish Puri) seeks to usurp the throne, revive his "fauladi shaitan" (a huge demon-like figure made of stone), and take over the world. The Vazir instructs his maids to strangle every child born to the Sultan.
Finally, however, a spark of divine intervention (presented literally as a spark which descends from the heavens and enters the womb) renders the next newborn son immune to the poisons and strangulations administered by the maids. This Shehezada (prince) eventually becomes Ajooba (miracle).
The Sultan and his wife Malika (Ariadna Shengelaya) kick off celebrations throughout the land. The good court magician Ameer Baba (Saeed Jaffrey), recently returned from his travels to the "land of Hind" (i.e., India), presents a magic sword to the Sultan.
The Sultan thrusts it into a pillar (verifying its keenness), and Ameer Baba pronounces that it may be drawn out of the stone again only by a member of the royal family (rather like the Excalibur).
Soon after, the Sultan privately discusses "traitors" with Ameer Baba. The Vazir overhears, eventually tricks Ameer Baba, steals his Necklace of Immortality, throws him into the dungeons, attempts to murder the Sultan and his family and takeover the throne. The Sultan escapes with his wife and child.
After a pitched battle involving magic carpets and storms and ships, the Sultan is missing, Malika is blinded, and the young Shehzada is washed ashore by a dolphin (whom he eventually thinks of as his mother) to a blacksmith. This blacksmith adopts the kid, trains him in all the worldly and martial arts, and thus creates Ajooba.
In the meantime, the Vazir blames Ameer Baba for the Sultan's murder, takes over the throne, and begins ravaging the land, always uttering his slogan Shaitan Zindabaad (Long live the Devil).
Ajooba is a masked rider in black (rather like Zorro) who thwarts the Vazir's lackeys as they pillage the lands and harass the citizens. His plain self is Ali, an ordinary restaurateur, and his chum is Hasan (Rishi Kapoor). Together they foil the Vazir's evil schemes, raid his caravans, and woo their girls. Ajooba, as Ali, falls for Rukhsana (Dimple Kapadia, actually the daughter of Ameer Baba, returned from Hind to rescue her imprisoned father), while Hasan's affections are for the Vazir's Shehzadi Henna (Sonam).
Ajooba inflicts constant pain upon the Vazir. The Vazir eventually raises his fauladi shaitan and plans an all-out attack. The Raja of Hind (Dara Singh, in a special appearance) brings his forces to aid Ajooba. The resulting war brings all the central characters together.
Several questions are essentially resolved in the ensuing war. The climax is a panorama of demons, magical horses and donkeys, a full-scale combat between the Vazir's army and the Hind army, enchanted swords, and a final revelation about the true identity of Ajooba.