The Starship Enterprise has responded to a distress call and has found that seven planets in a star system L370 are destroyed. A nearby system L374 shows that all but two have been destroyed. It then discovers a distress call from the Starship Constellation, and finds the ship in a terrible state. They send a boarding party, and find only its commanding officer, Commodore Matt Decker.
Records indicate the Constellation came across a large ship that was destroying the fourth planet, and that Decker beamed down the crew to the third. The machine then destroyed the third one, after disabling the Constellation. They evacuate the ship and take Decker to sickbay.
The machine reappears. Mr. Spock believes that it is a doomsday machine, built according to the theory of mutually assured destruction. Decker escapes from Sickbay and steals a shuttle, piloting it into the Doomsday Machine and exploding it. This fails to destroy it, but gives and Kirk and Scotty an idea - they beam aboard the Constellation and rig it to explode. At the last moment, Kirk beams off, and the Constellation explodes, destroying the Doomsday Machine.
The episode ends with speculation that if this really is a doomsday machine, there may be another of these things out there somewhere.