In some cases, Hanuman is considered to be an incarnation of Lord Shiva – and is called the “11th Rudra”.
After the war with Ravana and return to Ayodhya, and after reigning for several years, the time arrived for Rama to depart to his heavenly abode. Many of Rama's entourage, including vanaras like Sugriva decided to depart with him. Shunning the heavens, Hanuman however, requested to remain on earth as long as Rama's name was venerated by people. Sita accorded Hanuman that desire, and granted a boon that his image would be installed at various public places, so he could listen to people chanting Rama's name. From then on, he has become a Chiranjeevi and dwells everywhere Ramayana is heard / sung (in Sanskrit).
Hanuman appears in Mahabharata in the famous story where he subdues his brother Bhima’s arrogance and teaches him the value of humility. He also agrees to be on the flag of Arjuna’s chariot and is one of the three people who have heard the Bhagvad Gita directly from Lord Krishna himself, the other two being Arjuna and Sanjaya.