The Doctor stands outside a Western town called Mercy. A wooden sign says that it has a population of 81. Another sign says, "Keep Out". Rory points out this latter but the Doctor says he treats such things as suggestions rather than orders. Th town is encircled by a line of wood and stones. The Doctor steps over it and, followed by Amy and Rory, walks down the main street. A flickering street lamp attracts his attention: it is ten years before such a thing was invented.
They seem to be attracting angry glares from people on the street. When they enter the saloon, silence falls. The Doctor introduces himself. Someone asks if he is an alien Doctor. He agrees that he is. The occupants of the saloon rise up and grab the newcomers and bundle them to the edge of town. The Doctor is pushed over the boundary of wood and stones.
The cyborg appears in the distance and strides purposefully towards the Doctor. He shimmers out of existence and then reappears a few strides further on. The Doctor moves back towards the townsfolk but they pull out their guns and point them at him. The cyborg shimmers into existence a few strides nearer. The town marshal fires a shot into the air and tells the Doctor to get back across the line. As soon as the Doctor steps over the boundary the cyborg vanishes.
One of the townspeople says that he is an alien Doctor, "It could be him." the Marshal, Isaac, says, "You know it ain't." The Doctor follows the marshal back to the town jail. He asks what is going on. Isaac tells him that The Gunslinger appeared three weeks ago demanding the alien doctor. It was he who put the boundary round the town and nobody has been allowed in or out since. He throws a Stetson to the Doctor: it has a hole through the crown. He says that it was a warning shot to prevent anyone leaving. The plan is to starve the town until they give up the doctor.
Rory says that nobody could have known that the Doctor would be there because even they thought they were going to the Day of the Dead festival in Mexico. The Doctor blames their failure to arrive on toast crumbs on the console. He then sits down to work out what is happening. He says that the alien Doctor must be resident 81 in the town because the sign was recently altered. He must therefore be much loved and probably brought the electricity. If Isaac knew that the Doctor wasn't the alien then he must know who it is and where he is.
At this point a figure in the jail cell sits up and says it is time to end the subterfuge. He is a small, bespectacled man, apparently middle-aged. He has a distinctive tattoo on his face. He introduces himself as Kahler-Jex. The Doctor is delighted to see him, saying that the Kahler are the most ingenious race in the galaxy. Jex says that his craft crashed a mile outside town. When Isaac and the other residents pulled him from the wreckage he decided to repay them by becoming the town doctor: he was a surgeon on his own planet. Isaac says that he also saved them from a cholera outbreak. He adds that Jex provided the town with heat and electric light by using his ship as a generator.
When the Doctor asks what the Gunslinger wants, Isaac interjects. He says that it doesn't matter and that everybody deserves a second chance. He adds that the war only ended five years ago and there is still a lot of violence under the surface. He says that if they give up Jex, they will be opening the door to chaos.
The Doctor wonders why Jex didn't repair his spaceship. Jex says that it was too badly damaged. The Doctor then says he will pop out to the TARDIS and bring it back. He can then take Jex home. Isaac asks how he will get past the Gunslinger.
The plan is simple. Isaac (dressed in Jex's clothes) and Rory run out of town to distract the Gunslinger while the Doctor borrows the preacher's horse and rides out the other way. The preacher says that his horse is called Joshua but the Doctor says that he speaks horse and the horse is called Susan and wants the preacher to respect his life choices.
Amy is left with Jex. She wonders if he wants to return home. He says he has given everything he could to his people and welcomed the chance to start afresh on a new world. He notes that Amy is a mother; he can see it in her eyes. She asks if he is a father and he smiles, saying that in some ways he is.
As the Doctor rides into the desert he sees a cable and follows it to Jex's ship which seems remarkably undamaged. He uses his screwdriver to enter the ship. The internal alarm sounds, giving him ten seconds to enter a pass-code or the vehicle will self destruct. He manages to over-ride it before it reaches seven.
The alarm rings out across the desert. Jex realises that the Doctor is in his ship and isn't following the plan. It also draws the Gunslinger's attention away from his quarry. The Doctor accesses Jex's personal files and watches a harrowing sequence involving cyborg experiments, accompanied by much screaming on the soundtrack. As the Doctor leaves the craft he finds the Gunslinger outside, pointing his gun-arm at him. The Doctor says that he knows who Jex is and why the Gunslinger wants him. He doesn't understand why the Gunslinger hasn't entered the town to get him. The Gunslinger powers his weapon down and says that innocent people would get in the way. When the Doctor says they should return Jex for trial the Gunslinger says that when Jex starts to kill people on Earth they can use their system of justice. He adds that there will be no more warning shots and that the next person to leave town will be killed.
Amy finds herself at gunpoint, too. Jex has a revolver at her head. He says that certain truths may have been discovered and Isaac may not be so protective. He adds that, by taking Amy with him, he may be safer because the Gunslinger is less likely to shoot if innocent people could be hurt. Isaac returns with Rory and puts a gun to Jex's head.
Jex tries to say that he was leaving to get everybody else out of danger but the Doctor returns and explains that Jex is lying: he was responsible for murder. Jex argues that he is a war hero but the Doctor tells how Jex and his team took volunteer soldiers and fused them with weaponry to create a cyborg force. Jex says that a nine year war had killed half of the population on the planet. His cyborgs ended the war in a week. He argues that the morals of peacetime are not those of war and he refuses to repent. He goes on to say that when the cyborgs were decommissioned at the end of the war one of them went rogue and hunted down the team that created until only two were left. They fled but the Gunslinger has tracked them to Earth. Jex is the sole survivor.
Rory asks what to do with him: he is clearly a war criminal. Isaac argues that he saved the town from cholera. Amy asks when they started to execute people and turns to the Doctor who stands undecided. Jex looks at the Doctor and says that they are similar except the Doctor doesn't have the nerve to do what is needed to save his people. Suddenly, the Doctor bursts into life. He says they need to save the town and pushes Jex out to the border.
The townsfolk follow and when Jex tries to get back across the line the Doctor takes one of their guns and points it at him. Amy, too, grabs a gun and points at the Doctor. When the Doctor says she won't shoot she says that she's not sure, she may have been taking stupid lessons since they last met. She asks when the Doctor changed and decided that killing someone was an option. The Doctor says it is time to look after the victims; every time he shows mercy (to the Daleks, the Master) they come back and kill again. Amy says that the Doctor has been travelling alone too long and that they have to be better than Jex.
Realising she is right, the Doctor tells Jex to step back. However, the Gunslinger appears behind Jex and tells him to make peace with his gods. Jex tells him that he recognises the cyborg as Kahler-Tek. He says he remembers the names of all the cyborgs. He pleads that he has changed and is helping people. The Gunslinger pulls the trigger but Isaac flings himself at Jex, saving his life but taking the shot at point blank range. As he dies, Isaac tells the Doctor to save Jex and look after the town. He presses his badge into the Doctor's hand.
The Doctor tells two men to take Jex to his cell. He then tells the Gunslinger that things have gone on too long. The Gunslinger agrees and warns the town that if Jex is not given to him by noon the next day he will kill everybody.
The preacher arrives at the jail and tells the Doctor to go outside, adding that he should wear his gun belt. When the Doctor steps outside, a group of the townsfolk ask him to take a walk while they get Jex. The Doctor tells them that if he were to do that then Isaac's death would be meaningless. He adds that Jex might not be worth it but the townspeople are and he would hate to see someone as young as eighteen turned into a killer.
Later, Jex explains his guilt to the Doctor. The Doctor says that he thinks Jex has chosen his own punishment, exile on a backward world, because it is easier to confront than a trial on his own planet. Jex says that it would be easier to condemn him if he were merely a mad scientist who created a monster. He says that he believes when he dies he will have to carry the souls of all those he has harmed up a mountain. He doubts if he could be so strong. The Doctor says that they all carry their prisons with them. This gives the Doctor an idea.
The next day, when the Gunslinger arrives at high noon, the Doctor goes out to face him. Instead of drawing a gun, the Doctor produces a sonic blast from his screwdriver that distracts the Gunslinger. The Doctor has arranged for the townsfolk to wear paint in the shape of Jex's facial markings. While the Gunslinger tries to get a fix on the correct target they race between the buildings. This provides a distraction for Jex to get out of town and back to his ship. He does so while the Gunslinger is stalking through a terrified crowd in the chapel.
The self-destruct countdown begins when Jex enters his ship and he halts it with the pass-code. The Gunslinger, back in town, finds the Doctor. The Doctor tells him Jex is about to leave. Instead, Jex broadcasts to the Gunslinger, asking him if he will return home after all of this is over. The Gunslinger says he can't: he is a monster. Jex says that he is, too. He realises that Kahler-Tek will chase him wherever he goes and more innocents will be hurt. After a moment's reflection he decides that he needs to end the war. He restarts the ship's self-destruct sequence and is killed when his ship blows up.
The Gunslinger is ready to self-destruct out of town, saying that he is a creature of war with no place in a time of peace. The Doctor suggests it could be his role to preserve peace.
The Doctor offers Amy and Rory a chance to find out where all the animals sent up in the first rockets into space ended up. They decline his invitation.
The Gunslinger remains outside the town, Isaac's marshal's badge pinned to his clothes. The voiceover from the start of the programme is revealed to be the great granddaughter of the little girl in Mercy at the time. She says that they don't need a sheriff or a marshal for they have their own guardian angel from the stars.