Shortly after arriving at Egypt's notorious Tora prison, the Canadian film-maker John Greyson was beaten and kicked so hard that for the next week "there was a single bootprint perfectly etched on my back". His companion Tarek Loubani – a Canadian-Palestinian doctor – was subjected to the same brutal treatment. "We both went foetal to try to protect ourselves," remembers Greyson. "I was in pain for about a week."

It was not quite what Greyson and Loubani had envisaged when they arrived in Egypt on 15 August. The pair intended to remain only briefly in Egypt – their final destination was Gaza, where they planned to train and make a film about Palestinian doctors. Instead, they found themselves arrested during a crackdown on supporters of the ousted Egyptian president Mohamed Morsi, and spent the next 50 days detained without charge – most of it in a cramped jail cell shared with 36 other prisoners.

"There was no way you could sleep without touching your neighbour," said Greyson, of the conditions within their tiny 3-metre by 10-metre cell. "You had to co-ordinate with them when you wanted to roll over."

It was a nightmare that lasted until the early hours of last Sunday, when Egyptian prosecutors finally let the pair leave prison. It followed a high-profile campaign for their release – featuring a 150,000-strong petition signed by actors Ben Affleck and Charlize Theron – and finally intervention from the highest level of the Egyptian government. But even then their ordeal was not quite over. Still banned for several days from leaving the country, they were only allowed to fly home to Canada on Friday.

"We've switched from Tora prison to Novotel prison," Greyson joked in an interview with the Guardian while the pair were still in diplomatic limbo on Thursday, "and it's mostly a step up".

Speaking to a newspaper for the first time since their release from Tora, the pair's testimony shines a rare light on the abhorrent conditions inside Egypt's notorious prisons and police jails.

When Greyson and Loubani arrived at Tora, warders purposely left the three-dozen men inside the cramped truck, so that they might overheat in the blazing Cairo sun. One was on the point of a heat-induced coma before the truck's doors were finally opened.

Outside was just as frightening: two lines of policemen armed with batons and electric cattle prods who, Loubani said, "stood there beating people as they went between them". Shortly afterwards the pair were given the further assault that left Greyson with a boot-marked back. Loubani had received an earlier beating at a police station.

Conditions in their minute cell were appalling. The 38 detainees slept on concrete, with just one water tap between them. "Medical care was absolutely deplorable," Loubani said. One prisoner arrived with a broken foot that went untreated for three weeks until it got so bad it had to be removed.

"This young guy with a 100% preventable problem ends up having to have his foot amputated," said Loubani, looking thin and pale after six weeks on a prison diet. "That was a theme again and again – people were not treated."

The pair were keen to emphasise the plight of their fellow detainees, who were also detained without charge and have still not been released. Many have seen their lives fall apart while inside prison. "One guy was arrested on what would have been his wedding day," said Greyson. "Another man missed his child's birth. A lot of people lost their jobs, and some lost their homes."

All the cell's occupants – including Greyson and Loubani – had been arrested in an arbitrary roundup in central Cairo on 16 August and accused of attacking a police station during a protest. Many of them had either been nowhere near the station or were not at the protest in the first place.

For their part, Greyson and Loubani were not even aware of the station's existence. They were simply travellers in transit, with a day to lose in Cairo, and attended the protest merely to bear witness. Once the demonstration fell victim to Egypt's fourth state-led massacre in six weeks, they put their skills to work at a nearby mosque that had quickly transformed into a field hospital. Greyson documented the wounded on camera, while Loubani treated them. He could tell from the precise nature of their injuries that the dead had been killed by trained marksmen.

After the flow of bodies "slowed to a trickle" several hours later, the pair left to find their way back to their hotel. Unfamiliar with Cairo, they got lost in an area where many of the streets had been blocked off by a disorientating series of walls aimed at protecting government ministries.

When they stopped to ask for directions at a police checkpoint, their nightmare began. An officer grew suspicious at Loubani's Palestinian accent (as the home of Hamas, an affiliate of Morsi's now-hated Muslim Brotherhood, Palestine has become tainted by association with the ousted president). Like hundreds of others that day in Cairo, they were then hauled into state custody for little reason other than police hysteria and paranoia.

"It's fair to say that of the mistakes I made, identifying myself so clearly as a Palestinian was one of the biggest," said Loubani, who admitted that the pair did not fully understand the geographical and political context of their situation.

They kept trying to emphasise that they were not interested in Egypt, but merely on their way to Gaza – something that only made their captors more angry. "The minute you say Gaza, they say Hamas," said Greyson.

Once inside Tora, the pair formed good bonds with most of their cellmates. Both held English lessons for anyone interested, Greyson drew pictures for anyone who asked, while Loubani acted as the cell's doctor. "They adopted us," said Greyson. "There's a shared trauma that comes with being arrested, being hotboxed together, beaten together."

Guilty or innocent, the Canadians said, "all of these people are being treated unfairly. They don't deserve the beating, the terrible conditions, the lack of medical care. Our biggest call is this: charge them or free them."

Greyson and Loubani were themselves finally freed on Sunday after Loubani's father flew to Egypt to personally campaign for the pair's release. Building on diplomatic efforts and a huge pressure campaign back home, Mahmoud Loubani met senior officials before eventually and unexpectedly finding himself on speaker-phone with Egypt's entire cabinet. Following the phone call, the pair's treatment markedly improved and a few days later, at 1.20am on Sunday, they were finally released.

"Ever since I was a kid, my father always would come out and get me if I was in trouble," Loubani laughed. "And that's what he did this time too."

As they fly back, the pair are bracing themselves for a media frenzy in Canada, where their plight became a cause célèbre. But their thoughts are still with those left inside. "We saw a lot of people get massacred, and have their rights trampled in jail," said Loubani. "Neither is acceptable."