To defend themselves and keep predators away.
Skunks spray a mixture of chemicals from special glands under the tail. These chemicals mostly contain molecules with a thiol group (a sulfur attached to a hydrogen).
These chemical are known as mercaptans.
These are the molecules that humans can sense when only one molecule is present out of 10 billion air molecules.
A modification of these molecules is also present, where the thiol group has reacted with acetic acid to form what is called a thioacetate. These molecules have no smell, but break down into the mercaptans when they get wet. This is why a dog that has been sprayed by a skunk smells worse when the air gets damp.
Knowing that altering the thiol group removes the smell allows us to find remedies for skunk spray. If we oxidize the molecules with bleach, they lose their aroma. This is fine for washing clothes, but to wash a pet, we need something milder than chlorine bleach.
Hydrogen peroxide will work, but only if it is in an alkaline solution. So we wash the dog in a mixture of baking soda and peroxide, and much of the odor goes away. Repeating the wash helps, as does a final wash with a scented shampoo.