There are a number of possibilities for the cause of testicular pain including infection. Given your question concerning whether this could be sexually transmitted I’ll assume for the purpose of discussion that you are sexually active. Perhaps, one of the most common causes of testicular pain related to infection is caused by infection of a portion of the testicle called the epididymis. This is a small tubular structure that sits on top of the testicle and aids in the maturation of sperm. Infection of the epididymis can sometimes cause swelling and fevers, but almost always causes a degree of pain. Chlamydia or gonorrhea are two of the most common causes of this type of infection in sexually active young men, but other bacteria including many not sexually transmitted can also cause infection. There are non-infectious causes of inflammation and pain of this epididymis, as well, which can be related to trauma, bicycle riding or other activities.
Perhaps, one of the other serious causes of testicular pain is torsion or twisting of the blood supply to the testes. This causes the blood supply to be cut off and results in a great deal of pain. The onset of these symptoms is usually quite rapid. You can also, have torsion of a small part of the testicle called the appendix testis, which tends to be less painful and is usually less dangerous. Finally, other problems such as hernias, skin infections, or autoimmune disease can cause a painful scrotum. You should contact your primary care physician
who can discuss this more thoroughly and help direct you to the appropriate therapy. Good luck!