An unnamed man rides out of the desert into the isolated mining town of Lago in the American Old West. Three men follow him into the saloon, taunting him, then follow him to the barbershop. When they challenge him, he kills all three with little effort. Townswoman Callie Travers deliberately bumps into him in the street, knocks his cigar from his mouth, and loudly insults him. He drags her into the livery stable and rapes her. That night, in his hotel room, the Stranger dreams of a man being brutally whipped. The dream morphs into a flashback in which Jim Duncan, a federal marshal, is whipped to death in front of the hotel by outlaws Stacey Bridges and brothers Dan and Cole Carlin as the citizens look on.
The next day, Sheriff Sam Shaw approaches the Stranger and offers him the job previously held by the men he killed: defending the town from Bridges and the Carlins, who are about to be released from jail. He declines. Shaw, in desperation, offers him anything he wants in return. The Stranger learns that the townspeople were complicit in Duncan’s murder; they hired the outlaws to kill him after he discovered that the town’s only source of income, the mine, was on government land. They then double-crossed the hired murderers and turned them in.
Upon learning this, the Stranger accepts the job and takes full advantage of the deal. He appoints barbershop employee Mordecai as sheriff and mayor, and provides an Indian and his children with supplies at the shopkeeper’s expense. He orders the hotel owner and his guests to vacate the premises, leaving him its sole occupant. The hotel owner, Lewis Belding, and his wife Sarah, object. Callie and some of the resentful townsmen conspire to do away with the Stranger. Callie and the Stranger have sex in his room but Callie later sneaks out thinking he’s asleep. This is when some of the townspeople go after the Stranger in his hotel room, only to be blown up when he tosses a stick of dynamite into the room while they are beating a dummy in the bed, which wrecks most of the hotel as well. He drags Sarah, kicking and screaming, into her bedroom and they sleep together. The next morning, while discussing the imprisoned murderers, Sarah tells the Stranger that Duncan cannot rest in peace because he is buried in an unmarked grave outside of town.
The Stranger instructs the townspeople in defensive tactics, but they lack the competence for the job. He also orders that every building in town be painted blood red. Then, without explanation, he mounts his horse and rides out of town, pausing to replace “Lago” on the town sign with “Hell”. Meanwhile, Bridges and the Carlins have been released from prison and are heading to Lago. The Stranger harasses them with dynamite and long-range rifle fire, leaving them to ponder the identity of their attacker. Returning to Lago, the Stranger inspects the preparations—the entire town painted red, townsmen with rifles stationed on rooftops, picnic tables laden with food and drink, and a big “WELCOME HOME BOYS” banner overhead—then he remounts and departs again.
The Bridges gang arrives and easily overcomes the inept resistance of the townspeople. Bridges shoots several of the civic leaders who double-crossed them. By nightfall the town is in flames, and the terrified citizens are huddled in the saloon with the Bridges gang, who taunt them. A sound is heard in the street; Cole Carlin, while standing by the entrance to the saloon, is caught by a whip around the throat, dragged outside and everyone listens as he is whipped to death. Dan Carlin is then found dead too, hanging from another whip. As Bridges investigates, the Stranger appears, beats Bridges to the draw, and kills him.
On his way out of town the following morning, the Stranger pauses at the cemetery as Mordecai is finishing a new grave marker. “I never did know your name”, Mordecai says. “Yes, you do”, the Stranger replies. As he rides past a bewildered Mordecai to vanish into the desert heat haze, the writing on the new grave marker is revealed: Marshal Jim Duncan – Rest in Peace.