This curious phenomenon has been the subject of much discussion on internet forums, yet there seems to be no conclusive reason.
A frequent reason given is modesty. White shorts will go transparent when wet and will reveal what is underneath. By wearing a layer underneath that is darker or patterned, this problem is said to go away.
It is also claimed that swimming shorts will cling at the front, but by wearing boxers the clinging occurs out of sight. However, swimming shorts normally have an internal mesh liner, so this seems an unlikely reason to wear boxers underneath.
The next reason given in support, but boxers themselves give little support, certainly no more so than the mesh liner.
Comfort is cited as a reason to wear boxers, as the liner is said by some to feel uncomfortable.
Boxers may have to be worn as a punishment. This seems unlikely to be the case though, as most boys wear them voluntarily and appear to be enjoying themselves.
Boys may wear boxers because it has become the fashion to do so. It is not known where or how this fashion started.
Sometimes by convention a whole class will wear their boxers under their swimming shorts, without ever questionning why.
This may have originated as a form of attention seeking or even a risk/dare situation.
A school even may have a compulsory "boxers only under shorts" rule.
It is unclear whether boys pull their swimming shorts on over the boxers they already had on, or change into boxers specifically for swimming.
It is also unclear whether they change into dry boxers after swimming or keep their damp boxers on under their uniforms afterwards.
The feeling of putting swimming shorts on over regular underwear and then soaking it must feel erotic.
What is most interesting is that no boy ever seems to discuss it.