The Third Doctor and Jo Grant visit the remote Stangmoor Prison to examine a new method of "curing" criminality, whereby the negative impulses are removed from the brain using the Keller Machine to enact the Keller Process. Professor Kettering, who is managing the delivery of the Process at the behest of the absent Emil Keller, reconditions a number of inmates including Barnham, a hardened criminal who is reverted to a more innocent and childlike state by the Process. The Doctor’s suspicions about the Keller Machine are heightened following a string of deaths, including that of Kettering himself, which seem to occur when the Machine is operated. Each death seems to be triggered by visions of personal phobias – and the Doctor is seemingly threatened by an inferno when he gets too close to it.
Meanwhile, Brigadier Lethbridge-Stewart and the troops of UNIT are handling the security arrangements for the first World Peace Conference. Captain Chin Lee of the Chinese delegation, whose delegation leader is dead, is behaving strangely in an attempt to heighten tension in relations with the United States. It emerges that her actions are done under the influence of the Master. She uses the transmitted power of the Keller Machine in her plans against the American delegate, Senator Alcott, who barely survives her attack. Captain Chin Lee is deconditioned by the Doctor, and tells him that Emil Keller is indeed the Master, whom the Doctor had previously trapped on Earth by stealing the dematerialisation circuit of his TARDIS.
Back at Stangmoor a riot has broken out and resulted in a dangerous criminal who was next in line for the Keller Process, Harry Mailer, seizing control of the prison. Jo is briefly taken hostage, but she enables the guards to retake the prison. The Master, who had heard of the Stangmoor riot by eavesdropping on UNIT, arrives and meets Mailer, to whom he supplies enough small bombs for Mailer and his prisoners to retake control of the prison. The Doctor returns to the prison to be captured by the Master, who sets the Keller Machine loose on the mind of his old foe, weakening the Doctor considerably. The Master is losing control of the Keller Machine, which contains a dangerous alien Mind Parasite, and forces the Doctor to help him contain its power. This done, the Doctor is imprisoned once more.
The Master has come to Stangmoor to engage the prisoners as a private army, and uses them to hijack a UNIT convoy transporting a deadly Thunderbolt missile nearby. The stolen missile is then pointed at the Peace Conference and Captain Mike Yates, who was detailed with leading the convoy, is taken prisoner by the criminals. Left in the dark, the Brigadier decides the Thunderbolt missile must be in Stangmoor and comes to the rescue in a "Trojan Horse" style assault. UNIT troops take control of the prison, killing Mailer and the other leading rioters. A freed Yates makes contact to tell UNIT that the Thunderbolt is being kept in an abandoned hangar nearby.
The Keller Machine is growing stronger and breaks free of the temporary restraints placed on it by the Doctor. The Doctor contacts the Master, who has gone to the hangar with the missile, and offers to return his dematerialisation circuit in exchange for the missile. The Master agrees to this proposition on the guarantee he alone will come. The Doctor has worked out that Barnham, having been subjected to the Keller Machine once and having no evil in his mind anymore, is immune to its growing power and uses the prisoner as a shield in transporting the Machine to the hangar for his showdown with his enemy. In the ensuing fight the Thunderbolt is triggered and the Machine destroyed, but the wider devastation from the missile is minimal. The Master uses the chaos to escape with the dematerialisation circuit, killing Barnham in the process. He contacts the Doctor by telephone to taunt him that he is now free while the Doctor remains trapped in his exile on Earth.