The setting for the town is actually downloaded from Kirk's mind, but the Melkotians only had fragments of memories to work with; therefore the illusion has a surreal incompleteness to it – buildings are obvious façades, windows and pictures appear to float in mid-air, doors open to nowhere, etc. The landing party also see that their equipment has changed; phasers are turned into six-shooter pistols, communicators and tricorders are missing, and there is no longer a way to make contact with the Enterprise.

Kirk and his companions quickly realize they are now impromptu characters in a bizarre re-enactment of the legendary "Shoot-Out at the O.K. Corral". Kirk and his team were playing the role of the infamous Cowboys; Kirk as Ike Clanton, Scotty as Billy Clanton, Bones as Tom McLaury, Spock as Frank McLaury and Chekov as Billy Claiborne. They have to eventually face off the notorious lawman Wyatt Earp along with his brothers, Morgan and Virgil and their friend Doc Holliday. This was the team's punishment for trespassing and violating the Melkotian's strict privacy, a duel to the death, played out as one of the most famous, historical events in the Wild West, with the crew on the losing side.

They encounter the various inhabitants who treat them as their roles and cannot be persuaded otherwise. That is especially true for the Earps who are belligerently determined to kill them at the appointed hour, or sooner if they have an excuse. The crew try every means to prevent the fight, but nothing works. They are physically prevented leaving the town, the Earps cannot be pacified and the Sheriff refuses to interfere outside suggesting the unacceptable tactic of ambushing the Earps. The stakes rise further when Chekov is romanced by a local girl. She is harassed by an Earp who kills Chekov when he interferes. Yet for their loss, Kirk realizes that Chekov's character originally survived the gunfight, suggesting that the events can be changed around.

That glimmer of hope is dashed when an improvised, knockout gasgrenade they planned to use on their enemies totally fail upon testing. The failure thereby erasing their one possible advantage. Spock remarks that it should have worked, but the hour of the fight comes before he can explain his reasoning. When the crew refuses to go to the corral, they find themselves immediately teleported there and cannot escape.

The team fears for their lives, but Mr. Spock explains his realization. He notes that the gas bomb should have worked according to physical laws. Thus, when it failed, the physical laws were disregarded and when that happens, reality does not exist. In short, Spock surmises that this whole scenario is not real, but actually an elaborate illusion occurring in the minds of the crew-or to be more precise; it is only as real as their minds accept it to be.

While Spock's will is so strong that nothing can harm him, he has to convince the others of the same, but any lingering doubt will prove deadly, since the mind will make it real. Kirk orders Spock to mind meld with each member of the team, in an attempt to wipe away any and all doubt from their minds that the bullets from the Earps' guns are mere phantasms, and will pass through their bodies without injury.

The shootout replays as history dictates, but when the Earps fire their pistols, their bullets have no effect as Spock predicted. Kirk tackles Wyatt Earp but does not kill him and tosses his gun to the ground. After the fight is over, the Melkots return Kirk and the rest of the landing party to the Enterprise, including Chekov, whose attraction to the girl clouded his perception of the false reality enough for him to survive.

A Melkot appears again on the bridge main viewscreen claiming he is impressed that even though Kirk had the opportunity to kill the Earps, he chose instead to spare their lives. This act of mercy changes the Melkots' distrustful opinions of the Federation, and opens their doors to relations with them from then on.