Crystals are made of atoms arranged in repeating blocks called “unit cells,” where the atoms in each repeating block are arranged the same way. Piezoelectric crystals have asymmetric arrangements of atoms in each unit cell. At rest, these crystals are electrically neutral, because the charges of the atoms are arranged such that each positive charge is balanced by a negative charge (really this has to do with dipole moments, if you’re into that level of detail). But when you squeeze or stretch the crystal, you upset this delicate balance of cancelling charges as you move atoms out of place, causing the crystal to have net charge on its faces. Same deal if you apply charge: you apply electrical force that makes the atoms rearrange in an attempt to cancel the charge, causing the shape of the crystal to change. This effect can be pretty large: applying 500 foot-pounds of force to a cubic centimeter of quartz can produce 12500 volts!
It makes me wonder how many other things I use operate on near-miraculous science!