To distract Hanuman, Ram threw his ring in the underworld (Paatal) and asked Hanuman to get it. Ram then invited Yam, but he put forth a condition that the conversation between them must remain unrevealed and if anyone interrupts the conversation, Ram would kill that person. Ram asked Laxman to guard the gate to ensure no one come in to interrupt the meeting of Lord Ram and Yam.
In the meantime, angry sage Durvasa came in to meet Ram only is stopped by Laxman. Angry Durvasa warned that he would curse Ayodhya if not allowed to meet Lord Ram. Seeing the situation, Laxman decided to go and talk to Ram and hence interrupted the meeting. After this incident, to fulfil his brother’s promise, Laxman went to Saryu and gave up his life.
Laxman’s death before Sri Ram was necessary as the Shesh-Naag (the seat of Vishnu) that was Laxman had to return first, before Vishnu returned to Vaikunta. Vishnu here was Lord Ram.