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.
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 misaims and 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, 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.
Doctor Tom Baker (Fourth Doctor)
Companions John Leeson (K-9 Mk. II)
Mary Tamm (Romana I)
Lalla Ward — Princess Astra
John Woodvine — The Marshal
Davyd Harries — Shapp
Ian Saynor — Merak
Ian Liston — Hero
Susan Skipper — Heroine
William Squire — The Shadow
Barry Jackson — Drax
Valentine Dyall — The Guardian
John Cannon, Harry Fielder — Guards
Iain Armstrong — Technician
Writer Bob Baker
Director Michael Hayes
Script editor Anthony Read
Douglas Adams (uncredited)
Producer Graham Williams
David Maloney (uncredited)
Executive producer(s) None
Production code 5F
Series Season 16
Length 6 episodes, 25 minutes each
Originally broadcast January 20–February 24, 1979