One reason that cats roll around in the dirt is to coat themselves with bacteria that is needed for digestion. Cats lose a significant amount of bacteria during the grooming process. Rolling is also a side effect of catnip.
Female cats also roll around in front of male cats when they are in heat, as it is a signal that they are ready to mate. A cat in heat rolls around in the dirt, in grassy areas and on flat surfaces. If a cat has fleas or dry skin, they tend to roll around in dirt or grass to relieve itching and irritation. Dirt is also a safe way for cats to dry off after a bath or being caught in the rain. Cats do not enjoy being wet, and the dirt quickly soaks up excess moisture.
Dirt and small rocks provide a massage sensation as well, stimulating the cat's muscles and circulatory system. This sensation is quite pleasurable for cats, and it is extremely common for cats to roll around in the same spot each time in order to recapture the pleasant sensation.
It is important for pet owners to allow their cats to bathe and roll around in the dirt, as this is a completely natural habit. If a cat is not allowed to roll around in the dirt, they can develop anxious behavior traits.