What about having an urology test and you have to get naked? What about a doctor telling you that masturbation is good because it helps prevent prostate cancer? What if a doctor needs a semen sample for a test? Is it always a sin?
None of these things are sins.
By itself, being naked isn't a sin. It is embarrassing and we correctly avoid embarrassing ourselves or others. Medical practitioners go out of their way to avoid the embarrassing situations while doing what is necessary to solve your particular problem. See also: Is getting a physical a sin because the doctor will see your body?
Giving a semen sample is no more wrong than giving a urine sample if that is what the doctor needs to make sure you are healthy. Fortunately semen samples are almost never needed from most young men.
It has been noted in some studies that men who ejaculate more tend to have less prostate cancer. At this time it is a noted trend, but it isn't proof that one causes the other. But what is talked about is how often a man ejaculates, not how those ejaculations were triggered. Masturbation once in a while causes no physical harm. Morally, so long as it is not done while lusting after sin, it isn't condemned in the Scriptures.