The Trinity of Bhagwan Vishnu, Bhagwan Shiv and Brahma Ji do their duty to protect and preserve Dharma in all ways. However, it is not always through war that they do this, but by intelligence too. One such instance is when they had to adorn the body of a demon to protect the worlds from moving away from the path of Dharma and Karma.
In Kritayug, there was a demon named Gayasur who was known for his righteous rule and conduct. By performing severe penance, he gained the kingship of all the three worlds and gained a boon that anybody who would see him would be free from all karmas. This perturbed the Devas, as Dharma and Karma were together. Dharma would not be followed as people would stop fearing their karma and not perform good deeds. They approached the Trinity and requested them to stop Gayasur. The trinity decided to stop Gayasur, who although a righteous one was putting a hindrance in the rules of creation. They took form of three Brahmins and approached Gayasur and mentioned to him that they wanted to perform a sacrificial Yagn uninterruptedly for seven days and required a huge place. They asked to give his body to perform the sacrifice with a condition that he wouldn’t move for seven days. Gayasur agreed for it.
Gayasur spread his head in Gaya (Bihar) where Bhagwan Vishnu sat, his body into Nabhigaya (Odissa) where Bramha Ji placed himself and placed his feet in Pithapuram (Andhra Pradesh) where Bhagwan Shiv took a seat and the Yagn started. The Trinity performed the Yagn for seven days without an interruption. Gayasur kept the count of the days through the crowing of the cock crowing in the morning. Realising this, on the last day, Bhgawan Shiv took the form of a rooster and crowed before the night ended. Listening to the sound of the cock, Gayasur thought that it was morning and moved his body thinking that seven days were over. The Trinity immediately appeared in front of him and said that Gayasur had broken his vow of keeping his body still for seven days. They said the punishment was death for breaking the vow. The virtuous Gayasur agreed for this without any complaint. The Trinity were pleased with his righteousness and asked him for a boon. Then Gayasur told them that he had no personal desires, but wanted his name to be remembered along with the Trinty. He requested to adorn his body they did as Brahmins permanently. He requests to grant a boon that if one’s ancestors were given oblations (Shraddha kriyas) at these three places, then they would have no re birth.
Accepting his wish, Bhagwan Vishnu took the form of Gadadhar at Gaya, Brahma Ji is worshipped as Girijadevi at Nabhigaya and Bhagwan Shiv is worshipped as Kukkuteswar (the one who took the form of a cock) at Pithapuram. Thus, Gayasur was stopped by the Trinity not to hinder the rules of ceation, but also was blessed by the Trinity for his selflessness and righteous conduct.