Driving over the brow of the hill on the road leading away from the commando training centre in Devon, I felt like the happiest man who had ever lived. It was sunset, music was blaring, and I was thumping the ceiling of my VW Camper in unadulterated joy. I was free. After months of uncertainty, lies, imprisonment and shame, I finally had my life back, and it lay glittered with hope in front of me.

It was the mid 1990s. I had enlisted in the Royal Marines Commandos and, 30 weeks later, earned my green beret after completing the longest, toughest military training course that can be joined direct from civilian life. Of our original troop of 32, only 11 had succeeded and I felt enormous pride. But I faced a problem: the training had not worked. Officially I was a commando, but mentally I was not wholly with them - I remained too free-thinking.

Ironically, it was this aspect of my personality that had drawn me to the marines. They were interested in a certain calibre of soldier - intelligent, independent and mentally robust. I had needed to get away from my home town, where I was in trouble with a number of drug dealers, and the armed forces provided a good escape. But if I was to join up, I wanted to be the best, better than my army father had been. I read widely about various units and decided that the marines were most my style.

I enjoyed training immensely. Before passing out I had considered going a step further and applying for the Special Boat Service, the navy equivalent of the SAS. But during the famous 30-mile march across Dartmoor, which completed the commando tests, I suffered a painful fracture in my foot. I finished the march, but was on crutches for two weeks afterwards, and then, while all my mates were getting drafted to units, I was kept at the training centre in Lympstone for a course of physiotherapy and recuperation.

Too much sitting around and playing my guitar led my mind to wander. I began to question why I was doing this, and what I had really been trained for. I realised that I was a pacifist and that I did not want to follow orders. I considered a commission, for which I had been singled out during training, but felt that I no more wanted to give orders than to take them. I wanted to travel the world, but wielding a guitar rather than a rifle, and marching alone rather than in single file.

I had more than three years to go before I could apply to leave the service. I investigated buying myself out before then, but could not afford the required £42,000. I thought of going awol, but decided to find a safer way. Feigning mental illness, asthma or amnesia were possibilities. Drug-taking was another. A fellow marine helped me compile a list - a long list - of all the options. I worked through them repeatedly, playing out the scenarios in my head. Then, one day, I read the paper: three officers were being ejected from the RAF for being homosexual. As far as I knew, this had never happened in the marines but still, here was my escape.

I had leave for a long weekend and went to see my mother. I told her my plans, and after much persuading she agreed to help me. As I returned to camp, she rang my troop sergeant and told him that I was very distressed because some of the soldiers had discovered that I was gay and were taunting and threatening me. I waited patiently in my barracks for an order. That afternoon a marine marched into my room and escorted me to the guard room, where the sergeant asked me if my mother's allegations were true. I confirmed that they were. Ten minutes later I was up in front of the regimental sergeant major (RSM). He looked me up and down contemptuously three times and said slowly: "So, Logan, you're a fucking bender?"

"Yes, sir," I replied, suddenly finding it hard not to laugh.

"Right, Marine, take him away and get him into rig, and get him back up here in five minutes," said the RSM to my escort.

Then began two months of hell. I was locked in the guardroom for two weeks; my meals were brought to me; and I was allowed just 20 minutes' exercise in the yard each morning. Word spread quickly, and although my closest training buddies suspected that I was lying, other soldiers, from my troop and beyond, eyed me with disgust and called me names. Some men punched their fists in to their hands when I walked by. At times I feared for my safety; yet despite the taunts, those who mattered - notably my troop sergeant - treated me with kindness.

After a fortnight, I was sent home to await further news on the investigation. Apparently, reporters were beginning to sniff about, and it was best that I was sent away from Lympstone. They wanted to handle the situation properly and discreetly. A few weeks later I was called back and subjected to a thorough medical, followed by in-depth questioning by five officials and military police. They sat behind a long desk and I sat alone on a chair in the middle of the room. A list of acts, such as "oral sex", "anal sex" was read aloud and I was required to answer "yes" or "no" to each in quick succession. Perhaps naively, I had not planned for this uncomfortable and embarrassing interrogation and I went on to affirmative autopilot, answering yes to all without pausing for thought. They asked when I had discovered my homosexuality and how I had first acted on it. My responses were necessarily constructed on the spot, as I had not expected this degree of probing into my imaginary love life. To this day I cannot recall what I said or how I managed to compose myself to answer convincingly.

Once again I was sent home, where I waited for weeks without knowing what would happen. Eventually an officer wrote to me and said that the case had been sent to Whitehall. I waited nervously and began to fear that I would be caught out and sent to military prison. I heard that another marine was selling a camper van; I bought it in preparation for going on the run.

Then, one bright sunny June morning, the phone rang. "Marine Logan?" said a precise voice."Yes, speaking," I said with equal precision."This is Sergeant so-and-so from CTCRM [commando training centre Royal Marines]. You are to return to base on June 10 at ten hundred hours. You are to be administratively discharged from the Royal Marines."

I went crazy, jumping and whooping and crying and shouting "Come on!" very loudly. I couldn't believe it.

So I returned to camp. Somewhat facetiously, I wore a blue-and-white striped Ralph Lauren polo shirt with a paisley tie. The RSM checked me over before I went in to see the discharge officer, and I could see in his eyes that he thought I deserved a more severe punishment, but he settled for yanking my tie into a tight knot and sending me for a grade one crew-cut. I was heckled and jeered as I went to the barbers, but no one could touch me now.

Shorn, I marched back to the officer and was discharged within about a minute: I was marched in, I saluted, and I was told to leave camp immediately. They handed me a reference (which was positive and did not disclose the reason for discharge), I saluted and was marched out again. That was it. All the torment was over in a flash.

I never saw the full details of the investigation. I was never certain whether they believed me or not. For weeks various national papers asked me to talk about it and I agreed to meet a reporter from one. However, at the last minute I cancelled and bought a ticket to Australia.

I had left the marines to travel and be free, not to make money, tell more lies, or bring the issue of gays in the ranks to public attention. Most importantly, I wanted to cause no more trouble for a force for which I still had enormous respect. For all the name-calling I suffered, I remember most of my comrades as loyal and courageous and generous. I actually feel some shame for what I did. My life has been a bit of a mess since I left, as it turns out, and the pious among us would no doubt have something to say about that.

But in those moments when I look back and feel some anguish there is always one memory that makes me smile. It is the laughter on the faces of the men on the front gate as I drove to freedom in my bright pink camper van.

· David Logan is a pseudonym.

Do you have a story to tell about your life? Email it (no attachments, please) to

my.story@theguardian.com. If possible, include a phone number.