In Springfield Prison, Sideshow Bob calls local right-wing talk show host Birch Barlow and complains about being unfairly imprisoned. Barlow and Springfield's residents pressure Mayor Quimby into releasing Bob. Bob is soon unveiled as the Republican candidate for the Springfield mayoral election. Determined to keep Bob from becoming mayor, Bart and Lisa decide to campaign for Quimby. Unfortunately, due to Bob's charisma and Quimby's drowsy, overmedicated appearance at a televised debate, Bob wins the election by a massive margin, among other things gaining the elderly vote by agreeing to build an expressway named after Matlock, which Quimby had originally promised, while also staying and listening to the retirees long, rambling stories. Among those voting for Bob are Krusty, who, though Bob framed him for armed robbery, is "aching for that upper-class tax break" he has promised, and Homer, who disagrees with Bob's "Bart-killing policy" but approves of his "Selma-killing policy."
The Simpsons awake to find that their house is in the way of Bob's new "Matlock Expressway" and that it will soon be demolished, which will leave the Simpsons homeless. Bob also forces Principal Skinner to demote Bart to kindergarten at Springfield Elementary School. Bart and Lisa begin to suspect that Bob somehow rigged the election. Lisa goes through the voting records but is unable to find any proof that the election was rigged. Lisa receives a message from a whistleblower to meet at night in an underground parking garage, who turns out to be Waylon Smithers after Homer unintentionally exposes him from the cover of darkness due to his car lights. Bob's policies disagree with Smithers' "choice of lifestyle," so he tells Bart and Lisa to find a voter named Edgar Neubauer, who will lead them to evidence of electoral fraud.
Eventually, Bart discovers the name Edgar Neubauer on a tombstone at the cemetery. After he explains this to Lisa, she discovers that many of those listed as voting for Bob are in fact long dead, including her former cat, Snowball I. At the trial that follows after Bart and Lisa reveal their story to the public, they trick Bob into confessing his crime by accusing him of being Barlow's political puppet, resulting in Bob blurting out that he was indeed the mastermind. He then reveals that he did this because of what he perceives as the town's stupidity at voting for Quimby, the Democrat, whereas Bob claims they really desire and need "a cold-hearted Republican to cut [their] taxes, brutalize criminals, and rule [them] like a king." The court then strips Bob of his position and sends him back to prison. The Simpsons get their house back, Quimby regains his job as mayor, Bart returns to the fourth grade, and the "Matlock Expressway" is put on hold. Bob vows revenge once he escapes from his "cesspool" prison, though the viewers are shown he is now remanded to a minimum security prison for white collar criminals where the gate isn't even locked, and he quickly finds a new goal to be accomplished: helping the Yale alumni inmates beat the Princeton alumni at rowing.