On a desert on Earth, a cyborg hunts down a humanoid alien, Kahler-Mas. He finally runs Mas to ground and tells him to make peace with his gods. Mas points out that the gods were the cyborg's once, but it says no longer and shoots Mas as he goes for his weapon. Mortally wounded, Mas asks if he's the last and the cyborg tells him that there is one more: the Doctor.
The Doctor, Amy, and Rory arrive in the 19th century outside of the town or Mercy. A keep out sign is posted and a border of stone and wood has been laid around the town. As the Doctor scans it for energy emissions, the cyborg watches them from the hillside. The trio finds nothing and goes into town, and the people of Mercy watch from the shadows. The Doctor points out that the town has electrical street lamps some ten years before they were invented.
The trio goes to a saloon and everyone stops and stares at them. The Doctor orders tea from Sadie the bargirl and the town preacher asks who he is. When he says that he's the Doctor, everyone stands up and the undertaker, Abraham, starts measuring him for a coffin. A younger townsman, Dockery, asks the Doctor if he's an alien. Surprised that they know about aliens, the Doctor nonetheless admits that he is and the townspeople immediately grab him and haul him to the edge of the town. As they toss him out past the stone-and-wood border, the preacher warns that "he" is coming. The cyborg appears in the desert, flickering in and out of sight as he draws nearer. The townspeople refuse to let him back into town until Marshal Isaac arrives and orders them to let the Doctor pass. Once he enters the town, the cyborg disappears. Dockery tells Isaac that the Doctor said he was an alien doctor, but Isaac figures it's not the one the cyborg is looking for.
Isaac take the Doctor and his friends to the jail and he explains that the cyborg, who they call The Gunslinger, appeared three weeks ago. Since then, he hasn't let anyone leave or enter the town. He figures that the Doctor and his companions were allowed to enter because they didn't have any food. Isaac explains that the Gunslinger fires warning shots at anyone who tries to leave, driving them back. Amy wonders if it has any demands and the marshal says that he demanded that they turn the alien doctor over to him. The Doctor realizes that Isaac must know who the alien doctor is if he knows that the Doctor isn't, and the prisoner in the cell stands up and says that it's time for the truth. He steps out of the cell and introduces himself as Kahler-Jex.
The Doctor recognizes the Kahler race as an ingenious species and Jex explains that his ship crashed near Mercy. Isaac and the others rescued him and took him in. Since then, Jex has served as the town doctor to repay his depth. When the town was hit by an outbreak of cholera two years later, Jex managed to save them. He also rigged a cable from his spaceship to the town, providing heat and light. The Doctor wonders why the Gunslinger wants Jex but Isaac says that it doesn't matter. However, the marshal admits that some of the townspeople want to hand Jex over to save their lives despite all that he's done for them. The Doctor, suddenly incurious, says that he'll get the TARDIS and evacuate anyone, but isn't interested in the cyborg. Amy wonders why but the Doctor says that there's nothing to be curious about. Isaac asks how he plans to leave town safely and the Doctor says that he'll use sleight of hand.
Soon, Rory and Isaac run out of town, drawing the Gunslinger's fire. However, he hesitates to shoot when his internal tracking computer warns that he might hit innocents and he follows them on foot. They take refuge in a canyon and tries to get a clear shot.
The Doctor borrows the preacher's horse and rides out of town for the TARDIS. Meanwhile, Amy suggests that they take Jex home but he explains that he's already given everything good to his people. He wants to stay in Mercy and start fresh, helping the people. Jex realizes that Amy is a mother from her attitude, and says that he's a father in a way. As Jex waits, he notices the lights flickering.
The Doctor stops to examine something on the ground. He finds a cable leading from Jex's spaceship to Mercy and follows it back to the ship. When he tries to open it, the Doctor sets off an alarm that rings out all the way back to town. The Gunslinger hears it and goes to investigate, while Jex hears the alarm and warns Amy that the Doctor isn't sticking to the plan.
Once inside the spaceship, the Doctor discovers that the security system is counting down to self-destruct. He stops it and brings up the personal files that Jex has filed. The computer gives a list of subjects in an experimental cyborg program and reports that they are all deceased. The Doctor opens the file and watches in horror as images of terror and death fill the screen.
When Amy checks on Jex, he pulls a gun on him and apologizes, pointing out that things have changed since the Doctor didn't follow the plan.
The Doctor emerges from Jex's ship and the Gunslinger aims his arm-gun at him. The Time Lord says that he knows who both Jex and the gunslinger are, and asks why it hasn't gone into Mercy and killed Jex himself. The Gunslinger informs him that doing so would endanger innocents. The Doctor says that they can give Jex a trial but the Gunslinger says that it will mete out its own justice.
As Jex takes Amy as a hostage, he tells her that there are things in his past that even Jex wouldn't understand. Isaac and Rory return at that moment and the marshal asks Jex what he's doing.
The Gunslinger tells the Doctor that he has given his last warning and now the townspeople must bring Jex out. The Doctor returns to Mercy and confronts Jex, telling the others that the Kahler doctor is a murderer. Jex insists that he's a war hero, but the Doctor tells the others that Jex and his team created the Gunslinger and other cyborgs, turning Kahler volunteers into programmed killing machines. The Kahler doctor tells them that his planet had been at war for nine years and was losing, and the cyborgs he build saved his race. The Doctor angrily asks Jex how many volunteers died in the process but the Kahler says that the rules of peace don't apply in a time of war. He explains that the cyborgs were decommissioned after the war but one of them, the Gunslinger, went off-line due to damage. Since then, he has been hunting down the scientists who created him and kill them. Mas and Jex were the only ones who survived, fled off-planet, and crash-landed on Earth.
Rory wonders what they should do and Isaac points out that Jex saved many of the townspeople during the cholera outbreak. Amy insists that they should save Jex and asks the Doctor, who appears distracted and vaguely agrees with Amy. Jex says that looking at the Doctor is like looking in a mirror, and the only thing that separates them is that the Doctor doesn't have the nerve to do what has to be done. The Doctor rounds on him, grabs him, and drags him down the street.
As the townspeople gather on the edge of town to watch, the Doctor shoves Jex over the border. When the Kahler tries to come back, the Doctor grabs a gun and prepares to shoot him if he tries to return. Jex says that he wouldn't do it and the Doctor admits that he doesn't know for sure what he would do. Amy grabs a gun and shoots it into the air, causing everyone to duck. When she tells the Doctor that they don't kill people, the Doctor responds by telling her that he can save everyone by sacrificing Jex. For once, he wants to think of the victims instead of trying to understand the killers. Amy says that they have to be better than Jex and thinks that the Doctor has lost sight of his compassion because he has been traveling alone too long. The Doctor considers what Amy is saying and then says that they'll think of something else.
The Gunslinger appears, moving in and out of visibility, and the Doctor tells Jex to reenter the town. The cyborg gets there first and tells Jex to make peace with his gods. Jex promises that he won't harm anyone again and argues that he has already helped the townspeople, but the Gunslinger ignores him and fires. Isaac leaps in the way, taking the energy bolt in the chest. As he lies dying, Isaac asks the Doctor to protect Jex and Mercy. As the marshal passes away, the Doctor takes his badge and pins it on, and then tells the townspeople to take Jex back to the jail. The Gunslinger tells them that they have until noon the next day to give Jex to him or he'll kill them all.
That night, the Doctor is sitting in the jail, watching Jex. The preacher comes in and says that there's something the Doctor needs to see, and tells him to bring Isaac's gun with him. The Doctor discovers that the townspeople are ready to take Jex and turn him over to the Gunslinger. Dockery suggests that the Doctor just give them the cell key and walk away, but the Doctor refuses, reminding them of his promise to Isaac. Dockery draws his gun and aims it at the Doctor, but the Doctor reminds him and the others that Isaac didn't want the town to turn into a lynch mob. He warns Dockery that using violence will only create more violence, and that the boy doesn't want to become a killer. When Dockery hesitates, the Doctor promises that he will save everyone and will risk his life so that the townspeople don't become killers. The citizens consider what the Doctor is saying and then walk away.
Back in jail, Abraham brings coffee for the Doctor and agrees with his stand... and measures him for a coffin just in case. Once he leaves, Jex suggests that the Doctor should have let the townspeople give him to the Gunslinger. The Doctor asks what he wants and Jex speculates that the Doctor is confused because he is dealing with a man who on the one hand is a mad scientist but on the other hand is a dedicated healer. The Doctor angrily accuses him of seeking redemption by receiving adulation and warns that Jex doesn't get to choose how he pays his debt. The doctor admits that in his people's beliefs, when someone dies they carry the burden of all the suffering they've caused up a mountain. He admits that he will have a heavy burden indeed, including his friend Isaac, and that his guilt has imprisoned him. When he says that the Doctor is imprisoned in his own morality, the Time Lord gets an idea.
The next day, the Doctor steps out onto the empty streets of Mercy at noon. Most of the townspeople gather at the church to pray. The Gunslinger walks past the border into Mercy and confronts the Doctor. After the two of them stare each other down, the Gunslinger aims his arm-gun... and the Doctor triggers his sonic screwdriver, emitting a blast of noise. The Gunslinger winces in pain and the Doctor runs off while the cyborg fires wildly before gaining control of itself.
While Amy releases Jex, Rory and Dockery paint their faces to match the markings on Jex's face. They run through the streets and the Gunslinger's targeting computer fixes on the marks but is unable to reconcile the multiple targets. Realizing that he's been tricked, the Gunslinger switches to manual following and prowls the streets. A young girl in the church inadvertently makes a noise and the cyborg blasts through the doors. As he scans the crowd, he sees the girl, hesitates, and then leaves.
Jex hears the noise from behind him but the Doctor tells him to go so that he can save the townspeople. The Kahler doctor runs to his spaceship while the Doctor goes back, painting the same marking on his face. The Gunslinger soon finds him and demands to know where Jex is. The Doctor tells him that the Kahler has fled, but Jex announces himself over the loudspeakers built into the electrical street lamps. He asks the Gunslinger if he will go back to Kahler once he has killed the last scientist, and the cyborg admits that he has no home now that he is a monster. Jex admits that he can't go back as well and the Gunslinger vows to hunt him down no matter where he flees. Realizing what he must do, Jex triggers the self-destruct and thanks the Doctor and says that he hopes that the souls that judge him in the afterlife will be kind.
The ship's explosion is visible from town and the Gunslinger says that Jex died with honor. The Doctor offers to take him back to Kahler but the Gunslinger says that he has no place there in the peaceful reconstruction. As he starts to walk off into the desert, the Doctor suggests that there is a role he could play in peace... by protecting it.
Later, the Doctor, Rory, and Amy are preparing to leave. He's ready to take them on another trip but they ask to go home before their friends notice how fast they're aging. Once they leave in the TARDIS, the young girl from the church goes to the edge of town and looks up at the hillside. Watching over the town is its new protector: the Gunslinger, wearing a marshal's badge.