This can be answered two ways depending on whether you are referring to natural pearls or cultured pearls.
Natural pearls are formed completely by chance. This is why they are so rare. They are typically formed when a parasite makes its way through the outer shell of an oyster, through the mantle tissue picking up a few epithelial cells on its journey, to its resting place in the gonad. The epithelial cells picked up enroute then form the pearl sac that envelopes the parasite and forms a pearl.
Today we no longer rely on this chance occurence. Pearls are now always cultured. The reason some oysters, even after the culturing process, may not have a pearl is that some oysters will refect the nuclei and spit it out. A large percentage of nucleated oysters will do this. The only way to tell whether or not an oyster has a pearl is to x-ray it. We do this here in Japan. But, even with the x-ray it is impossible to tell whether or not the pearl is of good or poor quality.
This is the reason that the pearls are implanted 'as pearls' in the oysters that are sold as 'oysters with pearls'. If the oysters were sold with a pearl inside that the actually produced, it would be quite rare to find good-quality pearls. By implanting a pearl they know that there will always be a good pearl within those oysters.
The reason that the oysters are placed into the formaldahide solution is two-fold. Yes, it does preserve the oysters indefinitely. But it also kills the oyster and makes the oyster constrict, closing its shell after the pearl is inserted. This was the oyster can be opened in front of the customer as though it grew the pearl itself.