Why do you think Shakespeare inserts a comic scene in Act 5, Scene 1 of Hamlet?
Shakespeare is a master at juxtaposing comic and serious scenes, and the proximity of the funny gravediggers' scene to the hugely emotional, climactic scene of Ophelia's funeral is startling. It is certainly true that we could see the gravediggers working as 'comic relief'.
Yet try to put yourself into the shoes of an audience member. If Shakespeare has you laughing and relaxing into a comic scene, surely you are more off your guard, and likely to be moved by a very serious scene, if something awful happens. The juxtaposition of dramatic and comic provides an extra electric shock to the audience: as if you're laughing and joking with your mum in the car, and then suddenly you smash into a tree.
Shakespeare is a genius at manipulating the emotions of an audience - and this carefully placed comic scene, as well as providing an interesting reflection on mortality and a practical preparation for Ophelia's funeral maximises the heartrending power of the scene which follows it.