In their search for the final segment of the Key to Time, the Doctor and Romana arrive on the planet Atrios, which has endured a recent bombing by their neighbouring planet Zeos, with whom they are at war. The Marshal of Atrios is about to launch a counterstrike on Zeos, but the Doctor finds that Zeos is deserted save for the giant computer Mentalis, which is controlling the war. He also discovers that the true opponent is a third planet called the Planet of Evil, ruled by "the Shadow". The Shadow, an agent of the Black Guardian, has Princess Astra of Atrios captive, threatening to torture her if she doesn't give him the location of the final segment of the Key to Time (which she doesn't know).
After disabling Mentalis, with the assistance of Romana and K-9, the Doctor creates a substitute sixth segment out of chronodyne, which gives the Key enough power to create a time loop in which to trap the Marshal's ship (armed with missiles for the final strike) as well as the Mentalis control room (engaged in an automatic self-destruct sequence). On the Planet of Evil, the Doctor encounters an old Academy mate, a fellow Time Lord named Drax, who has been employed under duress by the Shadow but agrees to help the Doctor. Drax referred to the Doctor by the nickname "Thete", short for Theta Sigma. Drax also talked about him getting his doctorate. It wasn't until the Doctor reminded Drax that he was the Doctor, that he started to call him Doctor.
With K-9 and Astra now under his control, the Shadow wants the Key for himself, and forces the Doctor to retrieve it for him. The Doctor leads the Mute to his TARDIS and opens the door, but is suddenly shrunk to tiny size by Drax, using the dimensional stabilizer from his own (in-need-of-repair) TARDIS. (Originally, as implied by the Doctor's reaction, Drax was supposed to use the shrinking beam on the Mute, but deliberately shrinks the Doctor instead. Episode 5 ends on this scene, and it's only at the beginning of Episode 6 that Drax shrinks himself as well, having misinterpreted the Doctor's plan.)
The Mute returns to the Shadow with the Key, but the Doctor realises why the Shadow has requested it: Astra is the final segment, and is transformed in front of everyone. Using their diminished size, the Doctor and Drax smuggle themselves into the Shadow's lair inside of the now-restored K-9. Drax again uses the stabilizer, this time to return them to their normal size. The Doctor snatches the partially assembled Key and the final segment, and disappears with Romana and K9 in the TARDIS, assembling the Key after dealing with the Mentalis self-destruct (with the help of Drax) as well as the Marshal's missiles (using the TARDIS to deflect them onto the Planet of Evil).
The White Guardian appears to congratulate the Doctor on finding and assembling the Key to Time, and requests that it be sent to him. However, the Doctor, realising that it is actually the Black Guardian in disguise—due to his lack of compassion for the fate of Princess Astra—orders the Key to re-disperse. Enraged, the Black Guardian, now reverted to his true form, threatens him with death. In an attempt to shake him off, the Doctor fits a randomiser into the TARDIS console, sending it to an unknown location in time and space, and leaving the Doctor and the Guardian with no idea of where they are headed.