The Enterprise arrives at Elba II, a planet with a poisonous atmosphere, where the 15 remaining incurably insane patients in the Federation are kept. They have a new revolutionary antidote and Kirk and Spock beam down to deliver it to Dr. Donald Cory, the colony's governor. Cory reestablishes the colony's protective force field preventing transport, and discusses the case of his newest patient, Captain Garth of Izar, a former Starfleet captain. As they go to see Garth, one of Kirk's heroes, another patient, an Orion slave girl named Marta, insists that the captain is making a mistake. She insists that Cory isn't really Cory, and he notes that they can't figure out why.
They arrive at Garth's cell… where they find Cory suspended in the cell. "Cory" transform into Garth, who release the other prisoners from their force field cells. Garth stuns Spock and has his minions take him away, and then insists that he is the new master of the universe. Kirk convinces him to release Cory, but Garth reveals he destroyed the medicine. Garth plans to take control of the Enterprise and reveals that he can transform into Kirk's visage. After Garth leaves, Cory explains that the people of Antos taught Garth the gift of cellular regeneration, and Garth mastered the technique enough to become a shapeshifter. Garth has also developed a powerful new explosive, which he plans to use in his conquest.
Garth contacts the Enterprise and orders Scotty to beam him aboard. Scotty offers the sign but Garth can't respond with the countersign and claims he was testing his Chief Engineer. After cutting off, Garth flies into a rage but quickly recovers. Scotty doesn't believe it was a test and tries to establish contact without success, and puts a security team on standby. They have no way to penetrate the force field without killing everyone beneath it.
Garth goes to see Kirk and offer him dinner, and has Spock meet with them. At the meal, Marta dances and flirts with Kirk while Spock notes that they only need a brief diversion to lower the force field. Garth tries to convince Kirk to join his side and when Spock points out the illogic of his position, Garth has him taken away. Garth then tries to get Kirk to give him the countersign. When Kirk refuses, Garth uses a modified sonic treatment chair to torture Cory. Kirk still refuses and Garth tortures the captain in the chair until he passes out and Marta convinces him she can get Kirk to give up the information.
While Scotty tries to find another way through the force field without success, Marta tends to Kirk in a private chamber. He tries to convince her to get him to the Enterprise and she says she's arranged for Spock to be freed. However, she then tries to kill Kirk with a knife and an armed Spock arrives and renders her unconscious. They make their way to the control room and turn off the force field, and Kirk calls up to the ship. Spock suggests the captain beam up and he take command of the security team. Scotty asks for the counter sign and Kirk asks Spock to do it. "Spock" refuses and reenables the force field, and reveals that he's a shapeshifted Garth, and has given Kirk a discharged phaser. Kirk tries to get through to him without success and Garth forces Kirk to bow down before him then stuns him when he tries to drop the force field.
Kirk wakes up to find that Garth and his people are preparing the Fleet Captain's coronation. Garth offers Kirk the role of Crown Prince and has Marta present him with his crown. Garth has Kirk taken away to the control room and the captain tries to convince his two captors to free him without success. Garth arrives and shows Kirk his explosive, then opens a view screen to reveal Marta on the planet's surface, slowly choking to death. Garth puts her out of her misery, detonating a tiny bit of explosive in her necklace. The Enterprise crew detect the explosion and Scotty determines to cut through the force field at its weakest point on the far side of the planet.
Garth sends two of his people to bring Spock in to persuade Kirk. Spock renders them both unconscious and Garth quickly realizes the Vulcan is on his way. Spock arrives at the control room to find himself facing two Kirks. Both of them refuse to give the countersign for fear of revealing it to the other, and both warn that any security team could be walking into a trap. Spock notes that Garth must be generating vast amount of energy to maintain Kirk's form and decides to wait Garth out. Garth-as-Kirk attacks and the real Kirk fights with him. One Kirk wins and the other insists Spock must shoot both of them… and Spock shoots the winner, then drops the force field.
Later, Cory and McCoy administer the new medicine to the patients and Garth undergoes treatment in the unmodified chair. He starts to recover and Cory tends to him, while Kirk gently teases Spock about letting himself be knocked out to test which was the real Garth.