The guard at the entrance cocked his rifle with a click-click that echoed around the cell block. Then with his thumb he unlocked the safety catch, and begin swivelling the barrel of his Kalashnikov towards me. I was standing behind a metal grille in a dark cell and had nowhere to hide. He pointed the barrel towards my chest; then, instead of firing, he pulled his gun slowly upward, and laughed.

It was day one of my incarceration in Gaza, where I was held in solitary confinement, denied books to read and a pen and paper, at times placed within earshot of violent torture, and threatened with imminent death several times. It lasted less than a month in February and March this year – hardly comparable, I am aware, with, for example, the years of imprisonment and exile endured by Mónica González Mujica, the intrepid Chilean journalist who today (World Press Freedom Day) will receive a World Press Freedom award from Unesco. But for me those 26 days seemed endless. For most of that time I was convinced that death was the most likely outcome.

"You are not a witness, you are an accused!" the military prosecutor had yelled at me when I was summoned into his office alongside the military court. I had come to give evidence in support of Mohamed, a young man who was the subject of one of my films. He had been part of a militant group that had fired numerous rockets into Israeli civilian areas. I discovered when I returned to Gaza in 2009 that he had left the group, ensuring immediate antagonism from all militants, and had begun to criticise the efficacy of these attacks and the justification for them. "Our rocket fire at their civilians only gives Israel the excuse and the justification to attack us," he told me on camera.

Biggest risk

Was I right to film him? Did filming him put him in more danger than he already was? I would argue that it is the interviewee's decision, not the journalist's, provided he has had time to think about it and the risks have been made clear; and in this case being filmed was more of a protection than a threat – repressive regimes worldwide fear negative publicity, and the biggest risk to dissidents is to be "disappeared" without anyone outside their borders taking any notice of the (non-) event.

Not only is Mohamed convinced I was right to have filmed him, according to his lawyer, but so too is his older brother, who is fighting to save Mohamed from execution. Was I right to try to give evidence on his behalf, despite the risk of arrest? That is a dilemma that journalists occasionally face: should we try everything to protect the subjects of our films if they later get into deep trouble with the authorities? Of course we should. Journalists have a duty not just to tell the truth in their media outlets, I would argue, but also to defend those who have given them their stories.

I had written to the military prosecutors, to the court and to the Hamas leadership stating that I would give evidence. Banners around Gaza still proclaim: "We welcome foreigners and will keep them secure" – Hamas partly justifies its bloody overthrow of the rival Fatah forces in June 2007 on the basis of establishing law and order and security. And it did succeed in getting Alan Johnston, the BBC Gaza correspondent who was held for four months by a militia not affiliated to Hamas, released. But since then Hamas has locked up scores of journalists or closed down their news operations, although the foreign media have been left alone.

My six interrogators argued that I was a spy for MI6, and possibly Mossad as well, and, as such, faced a mandatory death penalty. I was even told I had been inside a Gaza City hospital during the Gaza-Israel war of December 2008 to January 2009, seeking to discover if the Hamas leadership were hiding there. In fact, of course, no journalists could get inside Gaza City during the fighting, and I arrived back in Gaza several days after the ceasefire. My 21-year-old daughter's Facebook entry showed her paragliding in Cape Town – this became military training in Haifa, according to my interrogators. I had filmed in the tunnels that link Gaza with Egypt, showing weapons smuggling. Yes of course – and so have most of the world's news organisations.

The allegations may seem laughable, but the chilling truth is that it really doesn't matter how ludicrous they are: if a regime is hellbent on turning a journalist into a spy, it can simply put him on trial in a closed court, announce a verdict, list the now "proven" allegations, and lock the journalist up – or worse. Who would know? My excellent Palestinian lawyer attended one seven-hour interrogation session, then was banned. So too was the British consul.

The guilt or innocence of the supposed spy was of course irrelevant to the political calculation being made. So was my past record of long-term support for the human rights of people across the Middle East and Africa. My exposure, for example, of the awful death of one 12-year-old boy in Gaza City during the recent war, shown on CNN, NBC News and Channel 4 News, ensures I still get hate mail from those who believe I have a sinister role as a producer of pro-Palestinian propaganda.

A key factor weighing against my release was the politics of opportunism. Coinciding with my arrest was the assassination of a top Hamas official in Dubai, in which 12 faked British passports had been used. The Hamas security services, internally accused of failing to protect their man, now had the chance to show strength – they could tell the locals, and their hardline Arab and Islamist backers, that they had now caught a "spy" and would execute him: scoreline Israel 1 Hamas 1.

After 21 days, I was therefore amazed when a top Hamas official turned up at my jail. He produced calling cards from Lord Steel and other British parliamentarians, who had come to Gaza against the advice of the British government; at their request, he had asked to see me and get me released. I was fortunate in having a long track record working for broadcasters such the BBC, Channel 4 News, al-Jazeera and Arte, and for newspapers such as the Times and the Guardian. My detention was always likely to create some sort of western backlash, though oddly politicians and figures such as Archbishop Desmond Tutu appeared to put more pressure on Hamas than most media organs.

It seems Hamas's desire to appear reasonable and credible in western eyes won the day – but at a cost to me. The secret deal was to release me, but to warn me that personal details – my home address and the whereabouts of my children – would be released to "other agencies who might take a more aggressive view of things" if I revealed too much about my prison conditions or criticised Hamas. In return they would drop the absurd claims of spying.

Journalistic freedom

In the end they reneged on the deal, even as I was playing my part at the exit from Gaza – by refusing to answer questions on my prison conditions or my opinion of my Hamas jailers. I simply said my release was a "great victory" for the freedom of journalists to report fairly and accurately irrespective of the dangers. I called on governments and armed groups to release more than 100 journalists who are held in jails across the world or have disappeared. And for governments to take their duties seriously by making clear to such regimes or groups that there are consequences to this repression of journalistic freedom.

My contribution in future will be to fight for the rights of my colleagues still held in jail; to fight to free, or save the lives of, those dissidents who are locked up after speaking to us, or face death, as does the reformed rocket-firer Mohamed; and above all not to be cowed into silence. Hamas, and all regimes and groups who behave repressively, can expect much more exposure from my pen and my camera. And, I hope, from us all.

Paul Martin will be answering questions after the premiere of his short film Mohamed: Dissident Under Fire at the Everyman Cinema, Belsize Park, London, on Monday May 17. Book on 0870 066 4777