Mandrake the Lizard King was an imaginary enemy that the First Doctor created as a child to combat his loneliness, the Doctor often going up into the mountains to face "Mandrake" - really a dead lizard pinned to a discarded engine part - with his deadly stick, until he began to attend the Academy and made other friends.
When Crozier used his machine on the Sixth Doctor, which caused the Doctor to briefly regress to a selfish, childish state (TV: Mindwarp), the essence of Mandrake entered the machine and became a mind parasite. When Crozier went to place Lord Kiv in Peri Brown, it placed itself in her mind. Believing itself to be a real enemy whom the Doctor had erased from all written history, Mandrake set out to try and create an empire for himself, poisoning Yrcanos and then trying to arrange a new marriage for Peri that would give him the chance to form a new army. However, this plan was hampered due to Peri's infection by Spectrox toxaemia rendering her infertile, as no ruler would willingly marry a sterile queen who couldn't continue the royal line.
When the Doctor came back for Peri, Mandrake left him in a cell for five years - publicly still acting as Peri - until he was ready to put the next phase of his plan into action by deliberately poisoning Peri. Having formed an alliance with two other rulers that they would become "her" husbands once "she" was in Dirani, Mandrake confronted the Doctor, pretending to be Lord Kiv, and tried to convince the Doctor to transfer "Kiv" into Princess Dirani. However, the Doctor tricked the entity by transferring Peri's consciousness into his body while he put himself in Peri's, reasoning that he would be better equipped to suppress the entity possessing Peri than she was. When the Doctor identified his enemy, Mandrake wanted to know why no-one had heard of him, and was shaken to learn that this was because he was a creation from the Doctor's imagination when he was lonely. Now knowing he was only a product of the Doctor's mind, Mandrake begged the Doctor not to leave him, but he proclaimed that it was once again time to put away childish things and sealed Mandrake away once again.