At some point in the past, Shahryar created the Arabian Nights, and came to be the main character in the Arabian Nights storybook.
During the events of Sonic and the Secret Rings, the Erazor Djinn's evil spirits appeared out of nowhere in Shahryar's palace. With his guards and servants having run away, Shahryar hid in a large jar. When Sonic the Hedgehog and Shahra came to save the king from Erazor, Shahryar's hiding jar tipped and broke, revealing him, and Sonic believed Shahryar to be Dr. Eggman. Shahryar, furious at Sonic's insolence, ordered his guards to seize him, only to remember they were gone. He then explained to Sonic and Shahra about what had transpired, but was then kidnapped by Erazor on a pterosaur.
The pterosaur eventually took Shahryar to Dinosaur Jungle. Seeing Sonic nearby, Shahryar told him to save him, just as the pterosaur dropped him from the sky. However, he was saved by Sonic, who reluctantly grabbed the king's robe from a cliff with his foot. Though Shahryar was furious at Sonic's treatment, he told Shahra when she asked that Erazor was heading to the Evil Foundry with his minions.
Other game appearances
During the "Desert Ruins Event" in Sonic Runners, when Shahryar's kingdom appeared over earth, the king was overthrown by Dr. Eggman and stowed away in a pot while the doctor assumed Shahryar's place to gather mini Erazor Djinns for world domination. After Team Sonic stopped Eggman, Shahryar came out of hiding and, with a bruised ego, begrudgingly allowing them to keep whatever treasures they had found as reward.
Much like his alternate reality doppelgänger, Shahryar is short-tempered and loud, though he does not have the evil ambitions that Eggman is renowned for. He expects to be treated as the king he is and has no tolerance for insolence or rude behavior, willing to seize his forces on those he dislike without a second thought. Also, like Eggman, Shahryar tends to overlook crucial details in his rashness, such as when he called for his guards only to remember that they had all left.
King Shahryar is based on the real King that was told in the story of the Arabian Nights.
This is the first character from the Arabian Night book that is a dopplegänger to someone in Sonic's world.