The Enterprise is approaching the planet Melkot and runs into a crystalline buoy. It moves in to intercept them and they come to a halt. It warns them off with a telepathic message which everyone hears in their native language. Kirk is under orders to contact the Melkotians at all costs and sends a message of peace, but receives no response.
Kirk takes down a landing party of Spock, McCoy, Scotty, and Chekov and find themselves in an endless fog that doesn't register on sensors. A Melkotian appears before them, proclaims that they are "disease," and says that Kirk's memories will serve as the pattern of their execution. They suddenly find themselves in an Old West frontier town, circa 1880. Their equipment has disappeared and they are armed with old-revolvers. They find a newspaper headline identifying the town as Tombstone, Arizona, on October 26, 1981. Kirk can't quite recall what the date signifies. The local sheriff, Johnny Behan, approaches them and addresses them as the Clantons and the McLowerys. Kirk recognizes the name and realizes that they're in Tombstone on the day of the Gunfight at the O.K. Corral… and the Clantons lost.
A man is knocked out of a saloon and the marshall, Morgan Earp, guns him down. They confirm the man is dead and realize that death is a reality there. They go into the bar where everyone greets them as the Clanton gang. The barmaid, Sylvia, kisses Chekov and recognizes him as Billy Clanton. Kirk notices Morgan Earp but Spock advises him to sit down without initiating a gunfight. Morgan comes onto Sylvia, and Chekov defends her. Morgan backs down, noting that he won't let them provoke him into a fight alone. He leaves and Sylvia rewards Chekov with a kiss. Kirk asks her to leave and they try to figure out the situation. The gunfight occurs at 5 o'clock and Kirk plans not to be there. Kirk approaches the bartender, but he insists that Kirk is Ike Clanton. The captain tries to explain but the bartender insists that he's Ike Clanton.
Kirk goes to the marshall's office and tries to convince the Earps that he's not Ike Clanton, but they figure he's joking. When Virgil tries to provoke him, Kirk disarms him and they refuse to gun him down if he doesn't draw. However, they warn him they'll kill him at 5 o'clock whether the draws or not.
At three o'clock they try to leave town, realizing there's no way they can create communicators to reach the ship. They try to leave town but force fields keep them from leaving. With time running out, they come up with an idea to create drugs out of the local venoms, while Spock makes up a crude gas grenade. McCoy goes to the barber to find the necessary drugs and is told to talk to the dentist: Doc Holliday. Holliday lets him takes the supplies but warns him he'll be gunned down at one minute after 5.
While collecting supplies, Chekov runs into Sylvia who talks about the upcoming dance and suggests they get married. Morgan Earp finds them and tries to take Sylvia away. Chekov comes after him and is gunned down. Kirk and the others hear the gunshot and come running, and the Earps and Holliday arrive as well and the Enterprise landing party is forced to back down.
Working against time, Kirk wonders if Chekov would still be dead if he hadn't ignored the Melkot warning. McCoy notes that Spock has no feelings, but Spock is more interested in pursuing the fact that Chekov's "character" Billy died… but survived in the original gunfight. With time running out, Kirk tries to get Behan to intervene but the sheriff refuses and tells him to stage an ambush.
Kirk returns as the others complete their gas grenade. When he learns it hasn't been tested, he insists they test it and Scotty volunteers. However, the gas has no effect. Spock realizes that something is fundamentally wrong with the laws of physics. Before they can pursue the matter further, they are all teleported to the OK Corral with only ten minutes remaining. Force fields seal them in and Kirk says they have no choice but to fight. Kirk warns that they don't have a chance against experience gunfighters, but Spock pursues the fact that physical laws don't work. He believes the entire thing is a mental projection, and that their belief in the situation will cause them to believe they are killed. Spock has enough mental discipline to totally disbelieve the situation, but the others don't. If they have even a single doubt, they'll die. As the Earps and Holliday approach, Spock mind melds with his companions and convinces each of them of the illusion of the situation.
The Earps and Holliday arrive and the landing party just stands there passively. The gunfighters open fire and their bullets have no effect. Kirk then fights Wyatt Earp in hand-to-hand, easily defeating him. Kirk draws his gun and considers shooting him… then throws away the gun.
Everything disappears and the landing party finds themselves aboard the Enterprise. Chekov is unharmed and doesn't remember anything except Sylvia, and they're back at the moment just before they encounter the buoy. They encounter the buoy again and it starts to emit radiation. It self destructs before they can do anything. The Melkotian then contacts them and queries them about their interest in peace. Kirk reiterates that they're on a peaceful mission and the Melkotian, convinced by their test, agrees to let them make contact.