According to the Oxford University Museum of Natural History, fossils form when the remains of plants or animals become encased in sediment. Over time, the organic material dissolves away, replaced by a mineral replica of the original remains.
When sediment hardens around organic remains, it creates a cast around the material. When the remains erode away, the cast serves as a mold. If mineral-rich water flows through the mold, it may leave deposits behind that crystallize into a hard stone residue. Over time, this residue can fill the mold, creating a stone replica of what once was there. Fossils are usually skeletal remains because those body parts tend to last long enough to form distinct impressions in the sediment while flesh rots away too quickly.