Ibrahim, from Gambia, paddled into what he thought were Spanish waters and phoned the coastguard, demanding to be rescued. They handed him to the Moroccan coastguard and he's now in Tangier. Amadou, from Cameroon, had tried to scale the border fence into the Spanish enclave of Melilla. "The Moroccan cops beat us with their batons," he says. He was taken across the border with Algeria, near the city of Oujda 75 miles (120km) away, and dumped there with 35 others. Now back in Morocco, he lives rough, in a forest, reliant on the local mosque for food.

Gathering testimony from these men, and others like them, is not easy. They hide in the slums and forests. They bear the trademark scars I have seen on destitute migrants on all the borders of Europe: scars from racist beatings; scars from scrambling across rubble to escape the police. They have the deep fatigue and torn clothing that come with a life lived mainly under starlight.

Morocco has become one of the main transit routes for illegal migration into Europe from sub-Saharan Africa. According to the latest report from Frontex, the EU's immigration body, about 1,000 people successfully swam, sailed or scrambled into Spain in the first three months of this year. But there are up to 20,000 at any one time trapped in what an Institute for Public Policy Research report calls the "myth of transit".

The EU is now effectively paying Morocco tens of millions of euros a year to keep migrants off its territory. A European Commission spokesperson refused to specify the current amounts, telling me the money is used to "enhance the capacities of Moroccan authorities … in different areas of migration, including border management".

The problem is the Moroccan police are consistently violating the migrants' human rights. The migrants are brought by gangs, either through Mauritania and up the Moroccan coast, or through southern Algeria via Niger or Mali. The most expensive route is by dinghy from Tangier. This costs €450 from local merchants – the package includes a life jacket and a paddle – and entails a frantic journey in the dark from a beach just north of the city. Every migrant I met in Tangier knew someone who had drowned.

To tell the migrants' story I had to work undercover. You need a permit to use a video camera in Morocco, and though I had applied for one in May, the authorities persistently asked for "more time to organise it". When I got to the city of Nador, in north-east Morocco, I realised what they'd been organising. The army is engaged in a major clearing operation of the mountainous forest near the border, where the migrants have set up camps. On the mountain road parallel to the border fence I saw soldiers – squads were stationed every hundred yards – monitoring every bend of the road and every drainage culvert beneath it, with one soldier always watching the gap intently. Above them were platoons moving in line to sweep through the vegetation.

A report from Médecins Sans Frontières this year detailed "a sharp increase in abuse, degrading treatment and violence" of the migrants by the police and by criminal gangs, including a "shocking" level of sexual violence. Among those I met, this has only boosted their determination to escape north.

Two specific practices demand an answer from the EU, which is funding this operation. The first is the alleged return of boat people picked up in Spanish waters to Moroccan territory, which violates the right of asylum. The second is the dumping of migrants detained in Morocco in wasteland across the Algerian border, which is clearly illegal. The irony is that Morocco itself is a major source of migration into the EU, both legal and illegal. The thousands of empty homes that scar the suburban wastelands of its cities attest to the departure of 4.5 million of its citizens. Some of these homes make ready-made slums for the African migrants to live in.

With the tea kettle boiling, and about 12 men lounging on battered bits of cushion and blankets in one such slum, in Tangier, they quiz me: "Why are Europeans so determined to keep us out?" I give them the brutal truth: because a lot of poor white people think you are coming to steal their jobs, reduce their wages and destroy their culture.

The men look puzzled. Ibrahim says: "But they came to my country. And they support the president, the asshole who is destroying it and making it impossible for us to live there." The men acknowledge the racism they will face if they get to Europe, but say it's worse here.

For most, it's not persecution but penury that drives them northwards. There's an economic pull factor. Mustapha and Josui are bricklayers from Dakar. They have a cousin in Limoges, France, and think they can get work there. With 78,000 illegal border crossings into Europe last year, they're right to think they have a better-than-negligible chance of getting there.

It makes sense for Europe to boost Morocco's capacity to police its side of the border. It's a semi-police state but lacadasical: police roadblocks are positioned every few kilometres, just far enough away for decency from the contraband gasoline stalls and the hashish smokers. But it makes no sense for Europe to tolerate the abuse of the migrants' rights. Unless, of course, as with the Greek asylum crackdown, the whole thing is designed to massively flout the official commitment to legal and humane treatment, and to deter travel. The better Frontex works, the more the pressure builds in countries such as Morocco, with scant regard for human rights and massive problems of poverty among their own populations. The difference is, once it's beyond the razor-wired borders of Europe, accountability – and even the ability to report the facts – disappears.

Paul Mason's report on Morocco will be shown on Newsnight, Wednesday at 10.30pm on BBC2