The Enterprise is sent on a surveying mission to the center of the galaxy to confirm the existence of the "creation point," the source of the explosion that created the galaxy, and may still be spewing out matter. As Spock begins the science survey, a matter-energy "whirlwind" forms, buffeting the ship. Scotty warns that they can't maintain power and Spock advises them to head to the "eye" of the whirlwind where they will be safe. As the crew is thrown about, Spock goes to the helm console and sets course to the center.
As the crew make repairs, the Enterprise finds a single red-and-white-striped planet at the center of the creation point. As he analyzes it, Spock warns that time and space may not function as they understand them. The power in the engines simply fades away as the natural laws of their universe fail to reconcile with the local reality. The life support systems shut down and the crew collapses, asphyxiating.
A goat-like humanoid creature suddenly materializes on the bridge and Kirk begs him to restore their life support. He does so with a gesture and introduces himself as Lucien. Lucien says that he knew humans would eventually come searching for them, and insists that he's their friend. He invites them to rollick with him and then teleports away with Kirk, Spock, and McCoy.
On the planet below, the group appears in a desert and their bodies start to come apart. Lucien restores them to their natural forms with a gesture but complains that it's not natural. A whirlwind springs up, revealing a child version of Lucien, and Lucien dismisses it as children playing. He then gestures and the crewmen find themselves sitting in a forest near a city. Lucien explains that he's translated his world of Megas-Tu into images that they can comprehend, and that his image functions on what they believe to be "magic." Their entire civilization is centered around the use of magic, from love potions to architectural wizards. Spock realizes that they passed through the creation point to another universe, where "magic" is a natural law. Lucien points out that they don't know magic, but he used it to save their lives.
Following up on Lucien calling them friends, Kirk asks how he knows them. Lucien conjures up images of his people, each one a specialist in a form of magic, had no rivals in their universe. They eventually passed through the creation point and found Earth, and settled there. The Megans worked from the shadows, advising humans and drawing upon the power of their universe to perform feats of magic. They eventually left, despite the fact that Lucien wanted to stay. He was the only generalist among them, and they considered him a troublemaker. When Kirk wonders why the Megans left Earth, Lucien avoids the question and tells them they must return to their ship. Before he sends them back, he warns them not to give their presence away.
When Kirk and the others return to the Enterprise, they discover that Lucien has created a fog-like barrier between the ship and Megas-Tu. Spock wonders why Lucien put it there and who he is hiding them from. When Kirk asks what systems they have, Scotty warns him that other than life support, all ship's systems are still out. They have no way to defend themselves.
Determined to find a way to protect themselves, Spock attempts to replicate a magic spell by inscribing a mystic pattern on the rec room floor. Concentrating and insisting that it is logical that his belief can alter reality, Spock manages to move a piece on a nearby chessboard. Once they hear of the successful experiment, the other crewmen begin experimenting. On the bridge, Sulu attempts to create a magical construct of a beautiful woman. He succeeds, but it explodes as Lucien takes its place and tells them that they should let him protect them.
As Lucien warns them that their magical workings can be traced, a voice booms out throughout the ship, announcing that the Earthlings have been found. This time the Megans are ready for human perfidy and promise to make Lucien and the Earthlings play. The Megans pull the Enterprise down into the atmosphere, splitting it apart. The crew find themselves imprisoned in stocks in a replica of Salem, Massachusetts, circa 1692. Lucien is there as well, similarly imprisoned, and the Megans have taken on the appearance of natives of the town. A prosecutor steps forward, saying that as a specialist in the ethics of magic, he will serve as judge.
Kirk asks what charges are filed against them and the prosecutor says that on Earth he was known as Asmodeus. He shows the crew how the humans tried to abuse the Megans' magical abilities to serve their own greed. The Megans refused and the humans condemned them as evil warlocks. The surviving Megans gathered in Salem but gave themselves away and were caught and executed as witches. Drawing on the last of their power, they returned to Megas-Tu. Lucien insists that they fled because of loneliness and fear, but the Prosecutor insists that their fears were justified. Now that one Earth ships has found them, the Megans fear that others will come and the same thing will happen.
Spock volunteers to act as defense lawyer, since he is not a human. The Prosecutor agrees and Spock calls Lucien to testify. When asked why he doesn't fear the humans, Lucien explains that they are like him: inquisitive and seeking companionship. On Megas-Tu, the inhabitants are each an island unto themselves. Next, Spock calls Kirk to testify. The captain suggests that humans have tried to change since Salem, and they now respect all life forms. Kirk invites them to confirm his testimony using the Enterprise data banks, particularly the Prime Directive, and the Prosecutor summons all the data and distributes it among his people.
After they review the evidence, the Megans vote and the Prosecutor informs Kirk that they will release the Enterprise. The records show that the passage to their universe was an accident, unlikely to be replicated. However, the Megans plan to imprison Lucien for all eternity, alone, because of his betrayal. Kirk objects, pointing out that Lucien is a sentient being who doesn't deserve the equivalent of a death sentence. The Prosecutor, surprised, asks if Kirk would be so quick to defend Lucien if he knew that he was known on Earth as Lucifer.
Unimpressed, Kirk says that none of them will play a part in harming another living being. The Prosecutor launches a fireball at Kirk, who ducks it, and Spock tells his friend to draw upon the magic of the universe. Kirk summons what magic he can but the Prosecutor draws upon the power of all of his people, warning Kirk that he can't win. The captain insists that he has to, and if he fails then the Megans will become as bad as the Earthlings that they fear.
Satisfied, the Prosecutor ends his attack and restores the Enterprise. Lucien is there, free, and admits that he knew the humans would come to his defense. The Prosecutor explains that the true test was of their intentions, since they couldn't tell if the records were forged. The Megans had to see if Kirk's compassion was heartfelt or feigned. The Prosecutor says that the Federation will be welcomed if they visit in future and Lucien conjures tanks of ale for everyone in a toast to friendship.
As the Enterprise returns to its universe, McCoy wonders if Lucien really was Lucifer, the Prince of Darkness. Kirk points out that it doesn't matter, and Spock notes that Lucien was Lucifer, then it was the second time he was cast out... but this time Kirk saved him.