Can you explain why Shakespeare should use comedy at this point in Macbeth in an otherwise not very comical play?
Yes, I can explain why Shakespeare uses comic relief when he does in an otherwise serious Macbeth. The porter scene begins Act 2:3. In a short time before the porter takes the stage Macbeth has killed Duncan, felt so guilty about it that he could not get himself to join in with an Amen when he hears someone praying, returned to Lady Macbeth having heard voices, is obsessing about the blood on his hands, forgot to leave the bloody daggers with the grooms, been chastised by his wife for forgettng the daggers, and felt deep regret for what he's done:
To know my deed 'twere best not know myself.
Wake Duncan with thy knocking. I would thou couldst!
The tension and excitement reaches great heights here. All of the above raise the tension to that high level.
But, a slow resolution to this tension is not possible here. Shakespeare has the discovery of Duncan's body and Macbeth's slaying of the grooms coming up almost immediately. Tension cannot stay at a high level forever, so Shakespeare has to lower the tension quickly so that it can be raised when the body is discovered. The porter scene accomplishes this. The porter's jokes and manner, etc., lighten the mood and release the tension. This frees Shakespeare to raise it again once Macduff has had enough of the porter's jokes and cuts through the small talk with: "Is thy master stirring?"
Incidentally, the porter scene also furthers a theme or two and provides unity: the idea of Dunsinane as a hell is introduced by the porter and equivocation is joked about.
I agree, the Porter scene (Act 2, Scene 3) signals a break in the tension, the horror of the moment. As soon as Macbeth murders Duncan in the wee hours of the morning when everyone else is fast asleep, there is a knocking at the entrance door of the castle (Inverness, not Dunsinane, which I'm sure dstuva meant, above). There was no way for the Macbeths to know that the King had asked Macduff to come early to wake him up, but there he is, as appointed, knock, knock, knocking.
The Porter is fast asleep and wants to stay asleep, and he dreamily and comically fantasizes who may be calling so early in the morning.
It's not just a simple scene of comic relief, however. The Porter's reference to Belzebub and the fact that, under his covers, he feels it is hot like hell in the castle, and when he finally gets out of bed says: "I'll devil-porter it no further" all these references add up to a direct reference to a very well known Mystery Play that had been performed all over England in the 1500s called, "The Harrowing of Hell." In this play, Jesus comes to the gated of hell in search of honest souls to free.
Here, then Macduff is seen as the savior of Scotland, which he eventually will be.