What might have caused the pain and swelling I had in my testicles?

See doctor. You may have epididymitis or some other inflammatory condition of your sperm-producing system. This might be sexually transmitted disease, or possibly just an obstructive disorder. In either event, seeing a good urologist (and most are excellent) would be beneficial.