The story
The earliest and most commonly known usage of this term is in the American cartoon Scooby-Doo, where it was used regularly by one of the main characters, Norville "Shaggy" Rogers. Possibly inspired by minced oaths like gadzooks or zounds.
Etymology from Wiktionary, CC BY-SA 4.0.