This is an excellent observation. Overtly, both plays within the play are meant for the entertainment of the nobility in both plays, however, we need to analyse the content of these plays more rigorously in order to understand how Shakespeare uses them in these incredible dramas.
What is interesting to note about the play of Pyramus and Thisbe in A Midsummer Night's Dream is that it presents a kind of alternate story of what could so easily have happened to Hermia and the Athenian lovers had things been slightly different. Let us note the parallels between the two plots: both sets of lovers find that their union is opposed by their family, and both run away to elope. Shakespeare seems to be deliberately taunting us with the tragic possibilities of Hermia and Lysander's plan. So easily, it seems to suggest, things could have ended very differently indeed. We can only wonder at the thoughts of the lovers in the final scene as they watch this alternate plot of what could have happened to them had they been slightly less lucky, or occupying a tragedy rather than a comedy.
In Hamlet, The Mousetrap is a play that is deliberately designed by Hamlet for a specific purpose, as he states in a famous quote in Act II scene 2:
I'll have grounds
More relative than this—the play's the thing
Wherein I'll catch the conscience of the King.
The play therefore is part of Hamlet's attempt to prove the guilt of his uncle with regards to his father's death. Here again, therefore, we see that drama eerily matches reality, providing us with another perspective, but in this case, it is the true one. We are led to believe that Hamlet's father was killed by a serpent as he slept. It is only the drama that clearly shows that this serpent was Claudius.
Both plays within plays therefore serve a very important function in terms of the way that they mimic reality and interact with it, showing both us and the characters different versions of reality and the truth.