Female dogs do this to each other as a sign of dominance. It is not so much of a sex thing as an 'I'm in charge here' thing. However it can be vaguely linked to sex in so much as if one bitch is in season/coming into season, this can 'set off' these sorts of games.
I think this is because in the pack, the lead female would be the only one allowed to have pups so any bitch coming into season would make the head-bitch need to assert her authority.
I've not seen this with horses but I imagine it is the same thing. I know geldings mount each other all the time but then you know what boys are like .