If an animal dies in the ocean, the body of the dead animal should sink to the sea floor. If the animal dies out of water, then its body may be swept into the water. Water plays the main role in the process of fossilization. In the presence of water, the soft parts of animal body rot away, leaving only hard skeleton.
The skeleton of the animal continues to be buried when sediments like dirt, mud, clay and sand are added to the sea floor. The sediments themselves determine the details of the fossils. The deposition of fine grained particles allows more details in the future fossil. During earth-shifting events the process of sedimentation occurs rapidly. The quick deposition of sediments prevents the decay during fossil formation.
As the process of sedimentation continues, more weight is added onto the skeleton. The addition of extra weight compresses the lower layer of the soil, because of which the pressure is created. The pressure creates the cavity (or natural mold), preserving the shape of the skeleton. When mineral rich water enters the cavity, the cast of the mold are formed. The shape of the cast is the same as that of the skeleton.
The rock surrounding the skeleton rises to the earth surface, during earthquakes and mountain formation. The rock is worn away by the rain and wind, leaving the fossil exposed.