What is the importance of the gravedigger scene in the story of Hamlet?
It's an absolutely key scene. Shakespeare is an absolute master at juxtaposing the comic with the dramatic: and the two gravediggers' comic dialogue which precedes Hamlet's entrance prepares the ground for real surprise when the same grave, only a few hundred lines later, becomes the site of an impassioned fight between Hamlet and Laertes.
It's often the name of the game in Shakespeare's "comic relief" scenes that the themes reflect in a comic way the wider themes of the play. Thus, the opening argument about whether Ophelia "drown'd herself wittingly" reflects the play's concern with suicide - as outlined earlier in the "to be or not to be" soliloquy.
The scene also achieves precisely the absurdity which baffles Hamlet: the juxtaposition of death with comedy. It's no accident that the gravediggers are designated as "First Clown" and "Second Clown". Thus we have jokes about the pre-eminence of death: the gravemaker's houses "last till doomsday." And death even visits Yorick in the scene - an absent clown, now just a skull, who Hamlet and the gravedigger remember as "a fellow of infinite jest, of most excellent fancy". Even funny men - even men who tell jokes - die.
It's a brilliant scene which focusses the theme of "death" ready for the play's final scene and Hamlet's death - and provides the final comic moment of the play before the denouement of the tragedy.
The beginning of the grave digger scene is a comedic one. It is true that Shakespeare's plays, even tragedies, have comedic moments to lighten the mood for the audience. It is a very difficult thing to accomplish and shows once more what an excellent writer Shakespeare was.
However, I think that the bigger importance of the scene is a revelation that Hamlet experiences as he stares at the skull of a jester whom he grew up with. He suddenly realizes the inevitability of death and that death does not discriminate among people.
Remember the lines where Hamlet speaks of Alexander the Great and how he concludes by saying that Alexander could now be mud, used to fill in walls to keep the wind out? Regardless of how great man is, he will taste death and he will turn to dust and become a part of the soil, nothing more. It this great truth that Hamlet thinks deeply about in this scene, pondering the strangeness of death.
Shakespeare often uses tertiary characters such as gravediggers, clowns, or country bumpkins for comic relief. Their bawdy dialogue is context for social criticism and is used to make a parody of manners (contrast of the social classes; identification with the human condition in general).
However, in 'Hamlet' the gravedigger scene stages Hamlet's discovery of the skull of the court jester he knew as a child and inspires his musings over the brevity of life. This soliloquy in turn serves as a precursor for his learning about Ophelia's death, as he unwittingly witnesses her funeral procession.