This question has already been answered a couple of times. Check out the URL addresses below to read the answers: there are some interesting "takes" on this scene with regard to its "comic" relief.
Sorry for the spelling mistakes. Didnt recheck it.
Do reply and give me feedback and let me know if the answer and links have helped or not.
Its a dramatic relief as it rpovides comic relief to the audience after much tragic occurence in the play. The audience has experienced a lot of emotions and characters deaths. The porter scene allows the audience to, infact, get distracted from the tragic events that have occured frequently. The poter scene is important also because it allows the characters to change or take a break in between. The characters have been on stage for some time and to allow them to change or to take a break in between the porter scene has been added by Shakespeare. It is extremely important because it causes the play to move forward. Time is used in this part of the play. Shakespeare chooses to use reasonable time to allow characters to perform reasonably on stage. Nothing is without reason.
The porter scene laso caters to the audience which were from a lower class during that era. The comic element is present for them as well. Moreover, the language is not in poetry hence allowing the lower class people to quickly and easily understand the porter. But the porter scene plays an important role as comic relief for all the audience after much dramatic effects have occured in the play.