During a battle in the Aragonese town of Saragossa (Zaragoza) during the Napoleonic Wars, an officer retreats to the second floor of an inn. He discovers a large book with drawings of two men hanging on a gallows and two women in a bed. An enemy officer attempts to arrest him, but ends up translating the book; the other officer recognizes its author as his own grandfather, a captain in the Walloon Guard.
A flashback then follows Alfonso van Worden as he appears with two servants, seeking the shortest route through the Sierra Morena Mountains. The two men warn him against taking this route because it runs through haunted territory. At a seemingly deserted inn, Venta Quemada, he is invited to dine with two Moorish princesses, Emina and Zibelda in a secret inner room. They inform him that they are his cousins and, as the last of the Gomelez line, he must marry them both to provide heirs. However, he must convert to Islam. He jokingly calls them ghosts, despite having told his servants with great bravado that ghosts do not exist. Then they seduce him and give him a skull goblet to drink from.
He wakes and finds himself back in the desolate countryside, lying next to a heap of skulls under a gallows. He meets a hermit priest who is trying to cure a possessed man; the latter tells his own tale, which also involves two sisters and a forbidden love. Alfonso sleeps in the hermitage's chapel, hearing strange voices at night.
When he wakes and rides off, he is captured by the Spanish Inquisition. He is rescued by the two princesses, aided by the gang of the Zoto brothers (Senor Zoto). Back in the inner room, the two princesses become amorous with Alfonso but they are interrupted by Sheik, who forces the captain at sword point to drink from the skull goblet.
Again Alfonso wakes at the gallows, but this time a cabalist lies next to him. As they ride to the cabalist's castle, they are joined by a skeptical mathematician, who remarks, > The human mind is ready to accept anything, if it is used knowingly.
This ends Part 1 of the film.
Part 2 is primarily filled with the nested tales told by the leader of a band of gypsies who visit the castle. A true frame story, or tale-within-a-tale-within-a-tale, the narratives intertwine so that later tales shed new light on earlier experiences recounted by other characters. Multiple viewings of the film are recommended to comprehend the plot and to identify appearances of certain characters before they are "introduced" by the gypsy raconteur.
Finally, Alfonso is told to return to the Venta Quemada, where he meets the two princesses. They bid him farewell and the Sheikh gives him the large book so that he can write the end of his own story. The Sheikh explains that the whole adventure was a "game" designed to test Alfonso's character.
Alfonso wakes under the gallows again, but his two servants are nearby—it's as if they are about to begin the journey he has just "dreamed." At the small inn in Saragossa, he writes in the large book until someone tells him that the two princesses are waiting for him. He flings the book aside, and it lands on the table where his descendant's enemy found it at the beginning of the film.