The story
From shoo + fly + pie. Named after the Shoofly molasses company, which was named after "Shoofly the Boxing Mule" (a circus attraction of the period), which was named after a Civil-War-era folk song called "Shoo Fly, Don't Bother Me". Not related to the idea of shooing flies away from the sweet filling.
Etymology from Wiktionary, CC BY-SA 4.0.