Mercutio's egotistical actions bring attention to himself. He tries to amuse the readers, himself, and other characters with speeches and jokes that are for his "innate interest". He spends a whole deal of time discussing his meeting with a fairy queen in a dream. His goal in this is to give himself what he wants, in a sense. Queen Mab "is the fairies' midwife, and she comes in shape no bigger than an agate-stone on the fore-finger of an alderman, drawn with a team of [tiny beings]". Queen Mab is for Mercutio's amusement and to show off his lighthearted spirit. This snippet of speech is completely irrelevant to the situation, which is the Capulet party. Such distractions scramble the concentration of the plot at the moment, literally driving attention towards Mercutio and away from the mainstream events.
Mercutio is killed to also make sure that the story goes the way it is meant to go. After his death, the entire situation of Romeo and Juliet spirals down into a depression caused by fate. The situation eventually turns into the tragedy readers know it as. Mercutio casts "a plague [t] both [the] houses" and dies, hinting that both families will suffer in a catastrophic way. Before Mercutio's death, the play can be considered a romantic comedy. After, however, the play is a tragedy due to the problems and misfortunes that follow. His lighthearted, jovial, and jocular attitude would never fit in the environment created by what the plot would have brought. He is the only one who would not be able to drive the plot as Shakespeare imagined. Because of this, Shakespeare is forced to kill Mercutio's character.