Fossils are usually created in sedimentary rock. Rivers and stream carry (transport) small pieces of rock. Eventually, these pieces of rock are dropped (deposited) to the bottom a the river or the lake/sea into which the river empties. Layers of deposited rock are called sediments and the process is known as sedimentation. Top layers of sediments push down on lower layers, Thus, the sediments then undergo compaction. During compaction, the water is removed and the sediments solidify and are squished together as rocks (sedimentary rocks). This process is known as cementation.
Sometimes, after an organism dies, it is quickly covered with sediments. Soft tissue may rot, but the hard bones and other structures of the organism are encased with the sediment. If weather or animals do not harm the encased organism, then a fossil forms. Fossils formed in this way are either cast or mold fossils.