When Stanley Yelnats is arrested for stealing the sneakers of famous baseball player Clyde “Sweetfeet” Livingston, the judge does not believe that the sneakers simply fell from the sky into Stanley’s hands. He is sent to Camp Green Lake, a correctional camp for juvenile offenders on an old dry lake in Texas. Here the boys are made to dig a hole each day in the desert, five feet deep and five feet across. The Warden claims that this is character-building, but it soon becomes clear that there is something else going on. When Stanley digs up a lipstick case, the Warden seems far too interested in where it came from. The adults at the camp appear to be looking for something.


Stanley’s family has a history of bad luck, beginning with Stanley’s great-great-grandfather Elya, who lived in Latvia. Elya wanted to marry a beautiful girl, but he had to give her father a fat pig in exchange for her hand. A gypsy called Madame Zeroni took pity on him and told him to take her runt and carry it up a mountain every day, to a place where the river runs backwards. He must then sing a lullaby to it. On the last day, he must carry Madame Zeroni up the same mountain and sing the lullaby to her. He did this, but on the last day he realised he did not love the girl after all, and he did not finish the ritual. Since then, Stanley’s family believe they have been cursed.


Stanley’s great-grandfather, also called Stanley Yelnats, made a lot of money on the stock market. Unfortunately, he was robbed by an outlaw called Kissin' Kate in the desert while travelling home. He took refuge on a mountain, while Kate buried the treasure for safety. Kissin’ Kate was once a schoolteacher in Green Lake, the lush town that existed on the site of Camp Green Lake before the water dried up. Katherine became the bandit ‘Kissin' Kate’ when the townspeople killed Sam, the man she loved, for the crime of kissing a white woman. Since that day, no rain has fallen in the area.


Back in the present, at Camp Green Lake, Stanley makes friends with a boy nicknamed ‘Zero’. He teaches Zero to read and write, and Zero helps him dig his holes. One day Mr. Pendanski, one of the adults at the camp, makes fun of Zero, insinuating that he is too stupid to teach. Zero loses his temper. He hits Mr. Pendanski with his shovel and then runs away into the desert. Stanley hears the other adults discussing what to do. They erase Zero, who is a ward of the state, from their records and conclude that he will die in the desert.


Stanley decides that he cannot abandon his friend. He runs away into the desert to look for Zero, and finds him hiding in the shade of an old boat. Zero eats spiced peaches from a jar he finds in the old boat and gets sick. Stanley carries him up a mountain, until he comes to a place with water and onions. He sings the pig lullaby, which has been passed down through the generations in his family. The onions and water help Zero to recover.


After a few days, Stanley and Zero return to the camp and begin to dig where Stanley found the lipstick. Stanley believes there will be treasure there. Sure enough, they unearth an old suitcase. However, before they have time to examine it, the adults find them. The Warden demands they hand over the case, but at that moment yellow-spotted lizards begin to emerge from the hole Stanley dug. Everyone freezes. Just one bite from a yellow-spotted lizard is instantly fatal, and the creatures are very aggressive.


They stand still for hours until the Attorney General arrives. Stanley’s name has been cleared and she is here to collect him. The Warden claims that Stanley was trying to steal her suitcase, but Zero points out that the case has Stanley’s name on it, and so must be his. The case does have ‘Stanley Yelnats’ written on it, as this is the money that Kissin’ Kate stole from Stanley’s great-grandfather over a hundred years ago. Stanley asks the Attorney General if Zero can come with them. She checks Zero’s files, but the Warden has already deleted all record of him. There is therefore no reason to keep him at the camp, and he leaves with Stanley. The Warden and the other adults, descendants of the townspeople who killed Sam and who have been searching for Kate’s treasure ever since, are punished for abusing the children at the camp.


Zero is reunited with his mother, and Stanley discovers that his father has invented a new foot-odour eliminator that, on top of the money from the suitcase, will make them very wealthy. It turns out that Zero is short for ‘Zeroni’; he is a descendant of Madame Zeroni. Stanley’s family's luck began to change the night he carried Zero up a mountain to a spot where the water runs backwards, and then sang the pig lullaby to him, thereby finally completing the ritual that Elya failed to carry out so many years ago.