The legend of Zhong Kui goes back to a Tang dynasty story of Emperor Xuanzong encountering first a small demon who stole his favorite concubine's embroidered perfume bag and his own jade flute and then a large demon who came to the emperor's aid by not only catching the small demon but gouging out his eyes and eating him.  When Xuanzong questioned this helpful demon, the demon introduced himself as Zhong Kui, a man who had committed suicide by dashing his head against the palace steps decades earlier on learning that he had failed the palace examination.  In gratitude for the posthumous honors the Tang emperor had then bestowed on him, Zhong Kui had vowed to rid the world of mischievous demons. 

Zhong Kui was often depicted in the company of the demons he had subjugated, as here.