At the height of the Maya civilization, in the city of Chichen Itza, a sacrifice is about to take place to prevent the end of the world from happening at the end of the 13th Baktun and the Mayan Calendar. A Mayan Homer, who has been fattened up, showing that he is ready to be sacrificed, hears about it for the first time (as he did not pay attention during orientation) and attempts to back out to no avail. However, his wife, a Mayan Marge, tricks a priest, a Mayan Moe, into getting himself sacrificed instead by promising him sex. After the sacrifice, a Mayan Professor Frink confirms that the world will end after the 13th Baktun, which, accounting for the Gregorian calendar and the birth of Jesus, puts the end of days in the year 2012 (with the Mayan Mayor Quimby placing the blame on President Barack Obama).
In the present, Homer encounters three Mayan stone gods, mistaking them for the trick-or-treaters. One of them crushes Homer underfoot, then the second one jumps on Flanders' house. The stone trio start to wreak havoc on Springfield and the Earth with one stone god throwing fireballs at Springfield City Hall and throwing Lard Lad's donut at a UFO and then taking this to popular landmarks like the moving the Eiffel Tower crashing to Big Ben, ripping up the Great Wall of China, causing the Great Wall of China to sink to a river of lava. And making the George Washington head kiss Abraham Lincoln's head in Mt. Rushmore; causing rivers of lava to appear, and splitting Earth into large fissures. Once their destruction is done, they high five to show their success on wreaking havoc in Earth, then fly off, only leaving the Earth to explode. It explodes, and the Earth is replaced by what appears to be the Earth's infrastructure or blood. The text the infrastructure or blood reads is "Treehouse of Horror XXIII ".
The citizens of Springfield gather to witness the activation of the Springfield Particle Accelerator; they originally wanted to use the money to build a new baseball stadium, but Lisa convinced them otherwise. Professor Frink activates the machine and it works, but nothing exciting happens, and everyone blames Lisa for her suggestion. When everyone is gone, two particles collide with each other and create a small black hole which floats off. Lisa finds it, and after it sucks up Ralph and Nelson, she takes it home so that it will not cause any more trouble. The Simpsons put it in the basement and Lisa warns them not to throw anything in it or otherwise it will grow bigger. Despite the warning, Homer, Bart and Marge use it as a trash disposal, and Homer even opens a business allowing people to throw their junk into it. The black hole becomes huge and consumes everything in sight. The only person who is not sucked in is Maggie, whose pacifier flies into the black hole, inexplicably stopping it. Meanwhile, all of Springfield has been warped to an alternate universe, where aliens worship their trash.
When strange events occur at the Simpson house, Homer sets up cameras to photograph what is haunting them. The culprit is revealed to be a Moe-like demon with whom Marge made a deal to save her sisters when they summoned the demon as part of a Satanic ritual. As part of the deal, the demon would return 30 years later to take Marge's favorite child as payment (which turns out to be Maggie, much to Lisa's shock). Homer manages to convince the demon to relinquish the bargain in return for Homer to reluctantly engage in three-way sex with him and another demon. After learning that the safe word is cinnamon, Homer throws his robe over the camera saying he'd like to try something and the demons are heard yelling 'cinnamon'.
Bart travels back to 1974 in Professor Frink's time machine to buy a comic book for 25 cents instead of the current $200 price at the Android's Dungeon. He then finds Homer in high school, just moments before he meets Marge for the first time (as seen in the season two episode "The Way We Was"). Before Bart returns to 2012, he tells Marge (who is already angry at teenage Homer for strangling Bart and constant demanding over her to be his prom date) to never get married to Homer. When Bart returns to 2012, he finds that Artie Ziff is now his father and the family is rich and successful, to the point where Nelson Muntz is now hired as Bart's butler and personal punching bag. 1974 Homer, who stowed away in the trunk of the time machine, finds out about Marge and meets 2012 Homer, who wants Marge. The two summon every time incarnation of Homer (dubbed "The United Federation of Homers Throughout History") to beat up Artie. Though the Homers lose badly despite greatly outnumbering Artie, they wind up winning over Marge, who then takes pity on the beaten Homers and lets all of them live with her.