Tom Sawyer is Twain's first attempt to write a novel on his own. He had previously written contemporary autobiographical narratives (The Innocents Abroad or The New Pilgrims' Progress, Roughing It) and two short texts called sketches which parody the youth literature of the time. These are The Story of the Good Boy and The Story of the Wicked Little Boy which are satirical texts of a few pages. In the first, a model child is never rewarded and ends up dying before he can declaim his last words which he has carefully prepared. In the second story, an evil little boy steals and lies, like Tom Sawyer, but finishes rich and successful. Tom appears as a mixture of these little boys since he is at the same time a scamp and a boy endowed with a certain generosity.
By the time he wrote Tom Sawyer, Twain was already a successful author based on the popularity of The Innocents Abroad. He owned a large house in Hartford, Connecticut but needed another success to support himself, with a wife and two daughters. He had collaborated on a novel with Charles Dudley Warner, The Gilded Age published in 1874.
He had earlier written an unpublished memoir of his own life on the Mississippi and had corresponded with a boyhood friend, Will Bowen, both of which had evoked many memories and were used as source material.
Twain named his fictional character after a San Francisco fireman whom he met in June 1863. The real Tom Sawyer was a local hero, famous for rescuing 90 passengers after a shipwreck. The two remained friendly during Twain's three-year stay in San Francisco, often drinking and gambling together.