|John R. Neill's illustration of the Shaggy Man in The Road to Oz, 1909.|
The first published appearance of the Shaggy Man was certainly in the first chapter of The Road to Oz where he meets Dorothy Gale and Toto on their Kansas farm.
But the earliest surviving script for what became The Tik-Tok Man of Oz is titled Ozma of Oz: A Musical Extravaganza in Two Acts and dated April 15th, 1909. It's labeled "second writing" and the Shaggy Man is a principal character in this script.
Which came first, the book or the stage script, both dated 1909? There's probably no way to tell now, but most likely L. Frank Baum was working on both projects at about the same time.
It's possible that Baum created the Shaggy Man for the play and then inserted the character into his manuscript for The Road to Oz. I think the timing makes it unlikely that Baum first put the Shaggy Man into The Road to Oz then later transferred the character to his musical script--but he could have.
We'll probably never know the sequence for sure.