Once upon a time, Sri Rama was seated in his council hall at Ayodhya after his return from Lanka. At the time, Deva Rishi Narada, Vishwamitra, Vashishtha and many others were present to give some important counsel to the king.
While discussing religious topics, Sage Narada said, "I request all of you present here to give your considered opinion in the matter as to whether the Lord's Name (Nama) or the Lord Himself (Nami) is superior." A heated discussion ensued. The assembly was not able to come to a definite conclusion. In the end Narada gave his verdict, "Certainly Nama is superior to Nami. I shall prove it now with a concrete example before the assembly disperses."
Narada then called Hanuman to his side and said, "O mighty hero, when you salute all the rishis and Sri Rama, do not salute Vishwamitra. He is only a Raja Rishi. He does not deserve equal treatment and respect." Hanuman agreed. When the time of salutation came Hanuman prostrated before all the sages except Sage Vishwamitra.
Then Narada said to Vishwamitra, "O great rishi! Look at the impertinence of Hanuman. He has made salutations to everyone in this assembly except you. You must punish him for this. Mark how proud and arrogant he is."
Vishwamitra, aflame with rage, said to Sri Rama, "O king, I have been seriously insulted by your servant Hanuman in the presence of these great rishis. Therefore, he deserves death at your hands before sunset tomorrow." Sri Rama had to obey Vishwamitra as he was his guru. At the same time Sri Rama was in a fix because he had to kill his faithful servant with his own hand. But he could not help it.
Hanuman too was very agitated. He went to Narada and said, "O divine sage, save me. Lord Rama will kill me tomorrow. I acted according to your advice. What shall I do now?" Narada said, "O Hanuman, never despair. Do as I tell you. Get up early in the morning at Brahmamuhurta. Bath in the Sarayu river. Stand on the sandy bed of the river and with folded hands repeat the mantra Om Sri Rama Jaya Rama Jaya Jaya Rama. I assure you, nothing will affect you."
The next day dawned. Hanuman went to the Sarayu before sunrise, bathed and with folded hands repeated the Lord's name as instructed by Rishi Narada. In the morning crowds of citizens came to witness the ordeal through which Hanuman had to pass. Sri Rama stood at a considerable distance from Hanuman, looked at his servant with pity and most unwillingly began to shower arrows on him. Not one could hurt Hanuman. All day arrows were rained on him but with no effect. Sri Rama even used the terrible missiles with which he had killed Kumbhakarna and other great rakshasas in the battlefield. At last Sri Rama took his Brahmastra, the unfailing missile. Hanuman repeated the mantra loudly with intense bhava and self-surrender to the Lord. He stood smiling at Sri Rama. Everyone was struck with wonder and exclaimed 'Jaya Jaya' to Hanuman.
At this juncture Narada went to Vishwamitra and said, "O sage! Control your anger. Sri Rama is tired now. The various arrows could not do anything to Hanuman. What does it matter even if Hanuman did not salute you? Save Rama from this struggle. You have now understood the glory of Sri Rama's name." Vishwamitra was moved by these words and stopped Rama from killing Hanuman with the Brahmastra. Hanuman came and fell at the feet of his master and saluted Vishwamitra for his kindness. Vishwamitra was very pleased and blessed Hanuman, praising his devotion to Sri Rama.
Sri Narada first gave this mantra to Hanuman when the latter was in great danger. So, O beloved aspirants, you also who are burnt by the flames of samsara may repeat this mantra and obtain emancipation.