Why did Hanuman fight with Lord Rama?
As far as I know, this story has no scriptural basis; it may just be folklore. The story as I remember it goes roughly as follows: the king of Kashi was once in Ayodhya, perhaps during the coronation of Rama, and he insulted the short-tempered sage Vishwamitra who was there at the time. Vishwamitra was furious and he told Rama that he wanted the king's head at his feet by sunset. Rama was duty-bound to obey, as Vishwamitra was his guru, so he promised that he would do that. The king of Kashi, terrified for his life since Rama was immeasurably powerful, sought refuge in Hanuman. Hanuman agreed to protect the king, and so Rama and Hanuman were forced to battle each other. Rama tried launching arrows at Hanuman, but the arrows fell to the ground when Hanuman kept chanting Rama's name, because Rama's name is even more powerful than his arrows. So they were at a stalemate, and then they found a way to resolve the situation: they made the king of Kashi bow down to Vishwamitra, so that technically his head would be at his feet.
I don't think this story is told in any Hindu scriptures; I originally learnt it myself from some comic book for kids. But I just found the story in the book "Hanuman's Tale: The Messages of a Divine Monkey" by Phillip Lutgendorf (pages 162-164):
[T]he king of Kashi happens to be journeying to Ayodhya to pay his respects to Rama. En route, he meets the sage Narada, who requests a favor of him. When the king agrees, Narada tells him to show respect to everyone in Rama's court, but to ignore sage Vishwamitra.... The ever-volatile Vishwamitra is enraged by the slight and informs Rama of it, calling it a stain on the honor of his court. Rama's brow darkens; he removes three arrows from his quiver and vows that he will take the life of the king of Kashi before the sun sets.... The king is trembling like a leaf, however, so Narada offers a suggest for how he might yet save himself: "Go to [Hanuman's mother] Lady Anjana and ask for her protection." ... When he then reveals that his would-be executioner is Rama hismelf, Anjana is troubled. "This will be difficult", she says, then adds, "still, I'll try." At this moment Hanuman arrives and bows at his mother's feet.... [S]he makes him, in turn, promise three times to assist her.... His mother reminds him of the great dharma of sheltering the week and of the fact that she has given her word. Hanuman says he will try to find a way out
Hanuman flies back to Ayodhya with the king and brings him to the bank of the Sarayu. He tells him to wade waist-deep into the water and remain there, repeating Rama's name. Meanwhile, Hanuman presents himself at court, bows to Rama, ... [and] asks that henceforth he be permitted to guard those who repeat Rama's name, and further, that as long as he does so, no power in the universe, - "not even God himself" - should be able to cause them harm. Rama readily agrees, whereupon Hanuman ... rushes back to the riverbank[.]... Rama is soon informed of this and becomes furious. He takes one of his arrows, charges it with mantras for the destruction of the king of Kashi, and fires it right from the palace. But the arrow halts in mid-air just in front of the king[.]...
Meanwhile, Vishwamitra too arrives on the scene, brooding over the curious crisis he preciptated. As Rama prepares to fire his arrow, Vashishta ... tells the offending king to throw himself at Vishwamitra's feet. The king does so, still repeating the mantra, and Vishwamitra is pleased and declares his act of repentance to be sufficient.
Note: “The question: Why did Hanuman fight with Lord Rama?” is licensed by Stack Exchange Inc (https://hinduism.stackexchange.com/); user contributions licensed under CC BY-SA.