Ala ud-Din has been imprisoned in a cave by his uncle, the Vizier, who plans to marry Ala ud-Din's love, Badroulbadour. When the TARDIS materialises in the cave, Ala ud-Din thinks the Doctor is a djinni and asks for three wishes. His wishes happen to coincide with what the Doctor needs to do anyway, so his efforts to convince Ala ud-Din that he isn't a djinni fail.
The Doctor is searching for an alien, who happens to be the Vizier. He defeats the alien, sending him back where he belongs, and Ala ud-Din is free to marry Badroulbadour.