Two reasons, either to relieve an itch or to mark with scent. For a dog, a great analogy is that they want their place to smell like home, which means it smells like them. [Figures we’re at opposites on this one. Here we are trying to rid our homes of any doggie smell and they’re doing their best to stink the place up!—Ed.] And when they encounter a weird smell, usually, they would like to pee on it, but they’ve been told not to do that inside, so they roll, marking the spot using the scent glands based on their tails instead.