Before we discuss this topic, please keep in mind that it's very much possible to reach different interpretations of and observations about literature, especially works as complex and layered as Shakespeare's Hamlet. So feel free to use this discussion as a starting point to come to reach your own observations and interpretations.
Of course, as we see in these two scenes in the play, Hamlet has a unique sense of humor. What's so nice about Hamlet's fun and sarcastic conclusions about people and incidents he observes is that he's able to reach some very interesting and ironic truths about life while criticizing them. In his sarcasm, there are lessons to be learned about human nature, society, classes, life, and death.
While observing the gravedigger, Hamlet discusses how people of wealth, high social class, virtue, and skill are now mere skulls thrown around without being given any care or importance. To me, this scene shows Hamlet is ever intrigued by the state of life and death. It hurts him to see people eventually end up in this state after death and are not helped by all they owned and accomplished in the world. He is clearly struggling with and trying to understand the meanings behind life and death. The scene also tells me of Hamlet's realization that the various social and economic classes are in truth meaningless, as none of it endures or avails one beyond death.
This realization is difficult to digest for Hamlet. He gives a lot of importance to wealth and classes. We see this when he makes fun of the gravedigger saying peasants like him are now going beyond their limits and showing their wit to noblemen like Hamlet.
When Hamlet speaks about Yorick the jester, we again see how difficult it is for Hamlet to accept and understand the transition of man from life to death to soil that might be used to patch a wall. This is Hamlet's realization of how little meaning and significance man and life hold.
In the Osric scene, Hamlet makes fun of Osric, who he says is not a gentleman or a nobleman and is just tolerated because he is a wealthy landowner. Osric tries to impress Hamlet and Horatio by using fancy words he does not correctly understand, and Hamlet makes fun of his attempt. In my opinion, this scene shows Hamlet does not evaluate people by their wealth, but maybe by their qualities and standing. Considering the gravedigger's scene and this scene together, we can argue Hamlet wants people to know their place in the social structure and not go beyond their limits. All people are not of equal standing to Hamlet; a peasant and a lord are not the same, and an uneducated or ignorant man is not worthy of respect just because he is wealthy. These two scenes help us understand Hamlet's opinion about social class.