The U.S.S. Enterprise is sent to planet Ekos to investigate the disappearance of an old Academy professor of Kirk's — historian John Gill. The Enterprise is attacked by an armed probe with a thermonuclear warhead, technology that is too advanced to be from Ekos or the nearby Zeon. Captain Kirk destroys the probe before it can do any damage, then takes the ship into orbit around Ekos out of range of the planet's detection devices. Spock tells him that, according to their records, the Ekosians are primitive, warlike people in a state of anarchy; Zeon has a relatively high level of technology, and its people are peaceful.
Kirk decides to beam down to Ekos with Spock; and Dr. McCoy fits them both with subcutaneous transponders. Scotty is ordered to use the transponders to locate and beam them back if they fail to make contact in three hours. When Kirk and Spock arrive on the planet's surface, a Zeon man urges them to hide. They watch as the man is arrested by officers wearing swastika armbands. Spock reminds Kirk that the Prime Directive prohibits them from interfering.
A viewscreen in the street plays news footage revealing that Ekos has adopted a military regime similar to that of Nazi Germany, and is now at war with Zeon. Kirk is puzzled that another planet could independently develop a culture identical to one that once existed on Earth. They watch a female Nazi officer called Daras receive a medal of honor, and learn that the planet is committed to the death of Zeon. The broadcast ends with a salute to the Führer — John Gill.
Kirk and Spock steal uniforms and assume the identities of Nazi officers in order to infiltrate government headquarters, but they are arrested before they can find Gill. Their phasers and communicators are confiscated, and they are tortured and interrogated. In jail they meet Isak, the man they saw being arrested earlier. Isak tells them that the Nazi movement on Ekos began when Gill arrived a few years earlier, and it is only a matter of time before Ekos invades Zeon.
Spock uses his subcutaneous transponder to make a crude laser that cuts open the cell door. They find their communicators in pieces, but Spock takes one and they escape. Isak takes Kirk and Spock to an underground resistance group led by his brother Abrom. Kirk explains to Abrom that he needs to find John Gill in order to end the war. But then the resistance cell is infiltrated by Daras, the high-ranking Nazi woman seen in the newsreel, who shoots Abrom. Kirk and Spock overpower and disarm her, but then they learn Daras is really a member of the underground, and the shooting was a ruse to test their loyalty. Isak and Daras explain that Deputy Führer Melakon is the one in command. Gill sees no one, but he is making a speech from the Chancellery. Daras agrees to help Kirk, Spock and Isak get past the guards. They dress as members of a Gestapo film crew and make their way into the headquarters. Isak wants Kirk and Spock to kill Gill as soon as possible, but Kirk refuses.
They find Gill in a booth surrounded by guards. Spock observes that he seems to be ill, or perhaps drugged. When Spock contacts the Enterprise with a repaired communicator, Kirk tells McCoy to put on a Nazi uniform and beam down. Watching Gill make his speech, McCoy confirms that he has been heavily drugged. Kirk, Spock and Isak overpower the guards so that McCoy can administer a stimulant to Gill, but he fails to revive him. Spock performs a mind probe on Gill, which brings him to consciousness. Gill tells them he used the example of Nazi Germany to bring order to Ekos. It worked at first, but then Melakon seized control, drugged Gill to use him as a figurehead, and started the war with Zeon. With another, potentially fatal dose of stimulant, Kirk keeps Gill conscious so he can make another broadcast from the booth. Gill announces that he has recalled the fleet and that the war must stop; he also tells the people that Melakon is a traitor. Melakon opens fire on the booth, and Isak shoots him. Gill is fatally wounded, but before he dies, he tells Kirk he was wrong to break the Prime Directive. But now he hopes the damage has been undone, and the Ekosians and Zeons will now work together.