When Niazullah left his home in the tribal areas of Pakistan in June, he figured he would be gone for five days, a week at most. The army had given a three-day warning before an offensive against Islamist militants in North Waziristan and had banned vehicle travel, so many families left in a hurry with just the clothes they were wearing.

Niazullah, 23, and his wife carried their two daughters and son through the mountain passes into Afghanistan, where they settled in a refugee camp in a flat and dry desert than 10 miles from the border. It wasn’t arable, but the land was vast and empty. A stream and craggy hills provided a natural defence.

There was only one problem. It was also a minefield. Had Niazullah asked the locals, they might have explained why the area had gone unclaimed. They would have told him that many shepherds had taken their sheep grazing there, and some had never returned.

Gulan refugee camp in Khost province is located on a former battlefield where the mujahideen resistance mounted its last defence against the advancing Soviet army in the late 1980s. To protect their supply routes, the mujahideen planted anti-tank mines, the size of large dinner plates, across an area of 100,000 square metres.

This was an effective deterrent against Soviet tanks but, decades later, it has made the clearance work “an absolute nightmare”, in the words of Farid Homayoun, Afghan programme manager for the Halo Trust, an NGO leading efforts to remove the mines.

A class held in a tent at the Norwegian Refugee Council funded school at Gulan refugee camp. Photograph: Andrew Quilty/Oculi

But Niazullah and his family had nowhere else to go, and so they decided to carefully pick through the dirt so as not to disturb the mines, and settle in among the groups of displaced tribes and families.

The fact that none of the 24,000 refugees in the camp has died is “fortunate and surprising”, said Tom Griffiths of Halo Trust.

At first, many newly arrived refugees did not believe the mines existed. “They would ask us, ‘Why are you bothering us? There is nothing here,’” Homayoun recalled. They were only convinced when the clearance teams began unearthing mines, 14 in total, and “their eyes just became wide open”.

Now they are aware of the threat, but regulating their behaviour is not easy, according to Homayoun. The rule was easy enough: cAlthough cars should travel only on designated tracks cleared by the experts, the rule is often broken.

Noor al-Huda, one of the mine clearers, pointed at a rutted track. “The path we are standing on right now hasn’t been cleared of mines. It is not meant for cars, only foot traffic. But as you can see, cars have driven on them, including yours. We see motorcycles and donkeys roaming around, and we worry. But there is nothing we can do. They are stubborn. It is hard to convince them to listen to us.”

Mir Fatteh Khan, a burly man of 40, worries about his 10 children, many of whom have taken to wandering the foothills in search of firewood. “We tell them not to, but they don’t listen. Children will just be children,” he said.

A sheet dividing boys from girls at the Norwegian Refugee Council school in Gulan refugee camp. Photograph: Andrew Quilty/Oculi

Huda and his team of 253 men hope to clear Gulan camp of mines soon. Until then, the residents will continue to lead a precarious existence. “We are just waiting for a disaster to strike. Every minute there is a possibility,” Homayoun said.

Over the past three decades of war, nearly three million Afghans have fled to Pakistan, where they share a language, religion and culture. Now the migration is being reversed.

The offensive that drove the refugees out of Pakistan was in response to an attack by Tehreek-i-Taliban on Karachi airport, and was meant to quell the Islamist militants who use North Waziristan as their base. The Pakistani military claims more than 1,000 militants have been killed since the assault began in June, a number that cannot be verified because of the government’s ban on reporters entering the area. In the process, 1.5 million people have been displaced, and as the military offensive enters its sixth month, more refugees are streaming across the border.

Among them are Niazullah’s cousins, who fled air strikes in Datta Khel, a district in North Waziristan. They brought stories of destruction that put an end to any idle thoughts of returning home, Niazullah said.

While the days are still blisteringly hot, at night the temperature can drop below freezing. Niazullah wore a set of traditional clothes, his only outfit. “We will be back in three months, or we will stay here for three years. There is no knowing what will happen,” he said.

At first glance, Gulan refugee camp appears chaotic. But in truth there is order: the refugees have recreated the system of tribal leadership from back home. Thirty members of a council of elders govern the camp, advocating for collective rights and punishing perpetrators of petty crime.

Despite earlier concerns, the elders say, there are no insurgent activities in the camp. The refugees are allowed to keep small arms, says Bo Schack, head of the UN’s refugee agency in Afghanistan, because “trying to make a community weapons-free where every man with respect for himself has to have a Kalashnikov is not possible”.

For children, who make up two-thirds of the population, life there consists of daily classes and making their own entertainment. A bit of plastic and string make for an ersatz kite. A brother pulls a sister down a hill on a homemade toboggan fashioned out of a discarded plastic pail. A boy has taken to collecting sticks near a cairn of rocks to build a model mosque.