Elba II, a planet with a poisonous atmosphere, also has a facility beneath its surface for the incurably, criminally insane. The U.S.S. Enterprise is bringing a new medicine with which they hope to eliminate mental illness forever.
When Kirk and Spock beam down to the facility of 15 inmates, they find that the asylum has been taken over by Garth of Izar, who was once a famous starship captain. He was driven insane by the terrible injuries he received while rescuing others. Garth convinces them that he is the head of the facility, in an attempt to gain control of the U.S.S. Enterprise and conquer the galaxy. He crowns himself, "Master of the Universe," and, when the officers won't be tricked into beaming him aboard the starship, he tortures Elba II's governor and then Kirk. He then sends his Orion mistress, Marta, out to the deadly surface above, only to "mercifully" spare her by blowing her to bits.
Spock, who had become separated from Kirk, returns to help the captain but finds two James Kirks. Garth learned the power of shape-shifting from a gentle race of beings that were unaware of his madness. When one of the Kirks offers to sacrifice himself to stop Garth, Spock realizes that this is the real Captain Kirk and subdues Garth. Garth is given the healing medication brought by the Enterprise crew and begins the long road to recovery.