It's a throw back from the Wolf ancestors of the dog.
The dominant animal raises their leg to pee, when marking.
It' not just a male dog thing, although male dogs will do it most frequently. Our female is the dominant dog and she raises her leg to pee too, when marking territory(on a walk through the woods for example). In wild wolf packs there are some packs that will have female Alphas(pack leaders) with no male Alphas. They breed with the Betas. This is not the majority but it has been documented, most recently in Yellowstone. It often happens when the Alpha male is killed or disabled. These dominant females will raise their leg to urinate when marking territory.
Dogs have such refined noses they can tell when an other animal has marked with urine, whether it was another dog or not, and who knows what other information. That's why they often mark over the previous marker, to stake out their range. In europe they are using dogs to smell human urine to diagnose certain illnesses that are very hard to detect in their early stages, extremely refined sense of smell like a catalog of information, so to speak. They can even detect specific metabolites of drugs in the urine.
When you see then "Scratch the ground" after marking with urine, it's because some dogs still have scent glands in their feet as do wolves, and this marks with yet another scent by expressing the scent from the glands when they "scratch the ground'.
Did you know that Wolf and domestic dog DNA is identical? They are all basically the same family, it's only the wild behaviours that have been bred out, along with appearance and personality. Some instinctive behaviours will remain.
Read some of the
works of David Mech, probably the most knowledgable Wolf biologist ever to exist. It's fascinating stuff.