The elephants are creating a mud bath which sticks to their skin and protects them from insects and sunburn. Elephants also roll in the dirt after bathing in water, which has a similar effect. I don't think one can test for reasons but an abductive argument (a.k.a. inference to the best explanation) might yield a reasonable evolutionary explanation.
To our surprise they used loose sand and a bit dry to sand bath while it was raining. The mud bath pool was less than 100m away from where they were sand bathing. So in the case of inference it could be that the sand acts as a substance that scrapes the skin wrinkles and washes the deep wrinkles on the skin. We thought they want to dry themselves but they did so while it was raining so even the sand got wet.