First off I will tell you that I am not a doctor nor do I have any medical training. So please do not take what I say as a diagnosis or medical advice.
If you could be a bit more specific about the pain such as when it occurs, how often it occurs, if it just when you expel gas, right after a bowel movement, how long the pain lasts, ect them perhaps some of us here at the forum could point you a little more in the right direction.
I believe I can answer your question about the gas issue though. In my almost 48 years I have passed a lot of gas and so has hubby. Neither of us have experienced the severe kind of pain you described. I have had trapped wind before but my symptoms for that were lower abdominal pain and when I massaged that area the wind was expelled.
Someone else replied that the cause might be Proctalgia fugax. Google nor askjeeves brought that term up but I did find it at another place. It is also known as rectal spasms so you might find more information if you researched that term. One word of caution (and boy am I guilty of this myself) is that you don't try to diagnose your symptoms, especially as severe as you describe by searching the internet and listening to the advice of us here at the forums (no offense to anyone here). I highly suggest that since the pain is so severe that you sometimes feel like you are about to pass out that you make an appointment with your doctor and have it checked out. It may be nothing serious at all and yet still it can indicate something more serious that might need medical treatment.
Just in case you could not find the info on Proctalgia fugax here is the definition for you:
Definition: Proctalgia fugax refers to pain (sometimes severe) in the rectum. It can last from a few seconds to a few minutes and often occurs spontaneously during the night. Pain sometimes accompanies orgasm and there may be an urge to defecate. The pain is sharp and gripping but remains localized. Relief is spontaneous but some people find also relief from warm baths, or ice cubes (anything to disrupt the spasms). Medications, such as diazepam, may be prescribed in cases that are very regular and/or severe.
I wish you the best,
I told my psychiatrist that everyone hates me. He said, "That cannot be true. You haven't met everyone yet!"