You have asked a really fascinating question. Lots of people overlook the interesting choice of Shakespeare in including a tragedy as his play within a play, and it is really important to consider the parallels between this play and the action of the main play. Don´t overlook the importance of this choice by the way the Mechanicals make a mockery of the tragedy!
For me, Pyramus and Thisbe is included because it offers a kind of alternate ending of the problem that we are presented with in Act I scene 1. Often, with Shakespeare´s plays, the line dividing comedy from tragedy can be quite thin, and his plays have elements of both. Act I scene 1 presents us with an opening that could be the start of a tragedy or a comedy in my opinion. Hermia is given a very stark choice - submit to her father´s will and marry Demetrius or face death. This parallels the situation between Pyramus and Thisbe (also echoing Romeo and Juliet), as both couples have a love that is not accepted by their parents or society at large. Both couples decide to run away to the forest to elope and marry in secret away from society, but of course, with Pyramus and Thisbe, this ends tragically, rather than the comic confusion with eventual happy ending that the main play presents us with. Whilst the main characters mock and poke fun at the Mechanicals for their over-the-top performance, the action they watch bears a somewhat disturbing resemblance to their initial problem.