It is created by SpongeBob using the magic pencil. SpongeBob adds it to Patrick's face because Patrick requests it. Soon after, it comes to life and flies away. It ends up on Squidward's head. It later flies off his head when Squidward falls while trying to pick up a fake dollar bill.
Somehow, SpongeBob later possesses the mustache, possibly unaware that it is the doodle mustache. He uses it for his pizza delivery sponge disguise to get into Pearl's party, but it eventually flies away.