Nucleus
All pearls begin with a nucleus, a tiny piece of debris like a piece of grit, a parasite, a decaying piece of a plant, or some other small particle. When the mollusk has its shell open to feed, it may accidentally take in some kind of debris, which then becomes embedded in the layer of fleshy tissue called the mantle. Ironically, this is how natural pearls begin -- with parasites, grit or other garbage caught inside a mollusk (usually a pearl oyster).
Nacre
Once the nucleus is inside its shell, the mollusk responds by trying to wall it off. The outer layer of the mantle tissue forms a pearl-sac around the nucleus, and the cells within this pearl sac start to secrete nacre. Nacre is a composite substance made of calcium carbonate (the same material you find in limestone) and protein. Together, these two components make a hard shiny material with a characteristic luster.
Formation
Over time, the pearl-sac will lay down more and more layers of nacre around the nucleus. This accumulated nacre with the nucleus trapped at its center is what humans call a pearl. Natural pearl formation is fairly uncommon; most natural pearls are small and have an irregular, non-spherical shape. The valuable ones, of course, are large and spherical, and these are even more uncommon. Sometimes the nucleus becomes attached to the shell so that only part of it is covered, forming a blister pearl with an irregular shape.
Pearl Culturing
Historically, pearls were very difficult to find; they were rare and there was no good way to predict where to find them or which mollusks would contain them. In the early 20th century, however, the Japanese found that by surgically inserting a bead of calcium carbonate and a piece of tissue grafted from another oyster, they could cause the mollusk to secrete nacre and create a spherical pearl -- the most desirable shape. This method remains the basis for pearl culturing today.