The song was written by Lee, Peart, and guitarist Alex Lifeson in collaboration with Canadian lyricist Pye Dubois (the lyricist of Max Webster), who also co-wrote other Rush songs such as "Force Ten," "Between Sun and Moon," and "Test For Echo." According to the US radio show In the Studio with Redbeard (which devoted an entire episode to the making of Moving Pictures), "Tom Sawyer" came about during a summer rehearsal holiday that Rush spent at Ronnie Hawkins' farm outside Toronto. Peart was presented with a poem by Dubois named "Louis the Lawyer" (often cited as "Louis the Warrior") that he modified and expanded. Lee and Lifeson then helped set the poem to music. The unique growling sound heard in the song came from Lee's fiddling with his Oberheim OB-Xsynthesizer.
In the December 1985 Rush Backstage Club newsletter, drummer and lyricist Neil Peart said:
Tom Sawyer was a collaboration between myself and Pye Dubois, an excellent lyricist who wrote the lyrics for Max Webster. His original lyrics were kind of a portrait of a modern day rebel, a free-spirited individualist striding through the world wide-eyed and purposeful. I added the themes of reconciling the boy and man in myself, and the difference between what people are and what others perceive them to be - namely me I guess.
Alex Lifeson describes his guitar solo in "Tom Sawyer" in a 2007 interview:
I winged it. Honest! I came in, did five takes, then went off and had a cigarette. I'm at my best for the first two takes; after that, I overthink everything and I lose the spark. Actually, the solo you hear is composed together from various takes.
"Tom Sawyer" begins in 4/4 before switching to 7/8 and 13/16 in the instrumental section. When the instrumental section ends, it returns to 4/4 before changing again to 7/8 for the outro.
Some of the Live performances starts with a short opening sequence with characters from the American TV show South Park. The sequence shows Cartman, Kyle, Stan and Kenny trying play the intro to Tom Sawyer as Lil' Rush, which ends up with Cartman getting the opening lyrics wrong and Kyle forcing Cartman to start again, as the boys just about to start the song again the sequence ends and Rush starts playing Tom Sawyer.