A Vizier has two sons: Shams al-Din Mohammed and Nur al-Din. When their father dies, they take turns being Vizier. They propose to marry two sisters on the same day so that Shams al-Din will have a daughter and Nur al-Din will have a son. Their hypothetical children will then be married, but the two brothers argue over a dowry price and part ways. As it happens, they get married on the same day, and Shams al-Din has a daughter and Nur al-Din has a son, Badr al-Din Hasan.
Nur al-Din becomes Vizier of another country. When Badr is twenty, Nur al-Din tells him about his uncle in Cairo and then dies. He loses favor with the Sultan and must flee the country. He falls asleep, and a Jinniyah and an Ifrit find him. The Jinniyah finds him beautiful, but the Ifrit speaks of a Vizier's daughter in Egypt who is just as beautiful. Her father (who is, of course, Shams al-Din Mohammed) insisted on marrying her to her cousin Badr, but the Sultan thought it was an excuse. Offended, the Sultan has arranged for her to marry an ugly hunchback.
The two djinn agree to compare Badr and the lady to see which is more beautiful, and take him to Cairo. They send him to a party with the engaged couple, where his handsomeness completely upstages the hunchback. After the party ends, the Ifrit tells Badr to wait until the Hunchback goes to the bathroom, and then take his place in the bridal chamber. As for the Hunchback, the Ifrit blockades him in the bathroom. The Hunchback says that he was forced to marry the lady, and the Ifrit tells him should stay in this room all night without making a sound and then leave never to return.
Meanwhile, as instructed, Badr tells the lady, Sitt al-Husn, that he's her real husband and it was a ploy to ward off the evil eye. They spend the night together but then the Ifrits send Badr off to a gate in Damascus. People think he's crazy, but a cook hears his story and adopts him. Back in Cairo, Shams al-Din Mohammed hears the bride's and the hunchback's stories and finds Badr's turban, money and letters, realizing that it was his nephew. Eventually, Sitt al-Husn has a son named Ajib. When he is seven, other children mock him for being a bastard. Shams al-Din asks for permission from the Sultan to go and find the boy's father.
They eventually reach Damascus, where Ajib and his Eunuch servant wind up at Badr's cook-shop, which he now owns. The fascinated Badr follows them, and Ajib strikes him on the forehead, but they later go back and eat there, and the food is delicious. Meanwhile, Shams al-Din finds his brother's widow, Badr's mother. When the family reunites for a meal, but Ajib is not hungry. They explain that they visited a cook-shop where the food was even better than Ajib's mother's. The Eunuch is tortured and told to bring back a dish. Badr's mother immediately recognizes the handiwork of her son.
The Vizier has Badr's shop destroyed and Badr arrested and brought in. The terrified and confused Badr is locked in a chest and finally wakes up in a chamber with Sitt al-Husn, who tells him the past ten years were all a dream. He feels the scar that Ajib left on his forehead. The Vizier soon enters, and explains that it was all a test to see if Badr was really his brother's son. The family is reunited.