All mammals breathe using lungs, unlike fish who usually breathe through gills. Thus, humans and aquatic mammals like dolphins, whales, sea lions and land mammals who jumped into the water are blessed with this natural reflex called mammalian dive reflex (MDR), telling our bodies to work efficiently underwater.
There are 3 major triggers for MDR:
- Facial submersion - little nerves on our face detect when it's submerged in water, it turns on MDR, and slow down our heart rate.
- Water Pressure - when swimmers dive down just 1 meter underwater there's changes of pressure detected,
- Breath holding - when mammals dive, they hold their breath, this trigger MDR
Now what MDR does is it gonna make us function more efficiently for the task at hand, especially when the water is cold or colder than your core temperature. The blood vessels in our extremities (like your arms and legs, fingers) will shrink, and oxygenated blood will be more in our vital organs which are heart and brain, because that's where the oxygen is more needed to keep your survival. This makes the blood volume in the heart increase. The body needs to regulate blood volume, so the brain took this signals as it has too much water, so body draw out water from your body and that's why you need to pee.
Now, in swimming, you may not feel so much pressure because you don't dive too deep, but the other factors are enough to trigger MDR. Apart from that, as another answer states, the sense of weightlessness also induces diuresis (increased production of urine), in swimming, buoyant body gives your brain sense of weightlessness. So those are contributing factors of why you urinate more after swimming than other sports.