Marine oysters and freshwater mussels produce pearls as a natural defense against irritants such as parasites or harm to their delicate bodies. Layers of aragonite and conchiolin, which also make up the oyster or mussel's shell, are progressively secreted.
This causes nacre, often known as mother-of-pearl, to form, which encases and protects the mollusk from the irritant.
Commercially the oysters are inculcated with an artificial irritant to produce nacre in huge quantities.
Nacre can spontaneously form around practically any irritant that enters the shell, resulting in some extremely rare and valuable pearls.
Pearls can be produced by other bivalve mollusks and gastropods, but they are not made of nacre.