The first purpose of this scene is what we call "comic relief". Many of the events leading up to this scene have been filled with drama and sadness. We have just learned Ophelia killed herself, Laertes and Claudius are planning to poison Hamlet, Hamlet has returned to Denmark and the audience is trying to adjust to all of this. In order to lighten the mood and give the audience a chance to absorb everything that is happening, Shakespeare inserts the comical grave digger's scene. This offers relief from all the sad events that have been occurring Yet is also offers some serious thoughts on life, its brevity and final outcome. This helps prepare the audience for the final scenes of the play.
Opinions on the purpose of the graveyard scene at the beginning of Act V differ from reader to reader. While the two gravediggers are designated "First Clown" and "Second Clown," the playwright's intention in presenting this scene may not have been to elicit laughter. Earlier references to "clowns" in Shakespeare's play, for instance, suggest the role of such figures in a traditional setting is the solicitation of laughter, as in Act II, Scene II, when Hamlet, in his conversation with Rosencrantz and Guildenstern, observes, "the clowns shall make those laugh whose lungs are tickled o' the sere." Even here, however, comedy is not the point of the young prince's observation, but, rather, the roles of various components of society. In Act V, Scene I, the two clowns are indeed enjoying a moment of merriment while they prepare the burial ground for the recently-departed Ophelia. Little about this scene, however, merits laughter. On the contrary, the fate of the beautiful young Ophelia is discussed in the context of her right, or lack thereof, to a proper Christian burial--an honor not traditionally bestowed upon those whose lives were taken by their own hand.
To this "educator," the primary purposes of this scene are twofold: Shakespeare wished to emphasize the contradiction inherent in the preparation of a Christian burial for an individual who had committed suicide, and, more importantly, the playwright was illustrating the leveling of humanity's playing field by emphasizing that, no matter our status in life, we all, ultimately, are returned to the dust from which we sprang. This latter purpose becomes apparent with Hamlet's arrival and contemplation of the skull that had, apparently, belonged to "Alexander." As Hamlet notes, "Alexander died, Alexander was buried, Alexander returneth into dust . . ." It could, therefore, be suggested that the main purpose of this scene, in which the two clowns continue their work while engaging with Hamlet and Horatio, is to emphasize the mortality of all people, irrespective of status.
This scene is also significant because of the comments made regarding the burial of Ophelia. According to the laws of the time a suicide death, which is what is believed happened to Ophelia, has to be treated differently than a death of natural or heroic causes. The suicide must be buried facing a particular direction and must be interred in a designated burial ground. There were also strict laws pertaining to who could bless or perform the burial rights over the body. If you reread Hamlet notice what the gravediggers are saying to one another regarding the upcoming funeral. Yes, there is joking but this important scene is much more than comic relief.