To understand how this is possible, you need to learn about quarks. By now, we know that some particles consist out of quarks. Some - because there are indeed elementary particles (electrons, neutrinos, photons, quarks and gauge bosons). But particles like neutrons, protons and mesons are compound particles. There are 6 quarks - up (u), down (d), strange (s), charm (c), beauty (b) and top(t), and corresponding to them antiquarks.
Quark composition of proton: p=uud; neutron: n=udd. Mesons are particles that consists of only 2 quarks. The lightest mesons are pions.
As an example, we can analyse reaction
As every particle-antiparticle pair, quarks can annihilate, and as opposite quark-antiquark pairs can be born when there is enough energy for creation of its bounded state. Neutral pion is a mixture of two states. As you can see, quark composition remain the same except pair that appears because the total energy of is enough to create it and, therefore, enough to born heavier particles .
Comments