TORKHAM, Pakistan — Thousands of Afghans gathered at the Pakistani border to return home on Tuesday as Pakistan temporarily reopened two main crossings that had been closed last month after a wave of militant attacks.

The Torkham and Chaman crossings were to remain open through Wednesday for nationals from both countries with valid visas who want to return home, a measure intended to calm tensions and ease a backlog.

“I have a valid visa and I promise that I will never come back here. Please allow me go back to my country,” Matiullah Khan, 52, told The Associated Press as he and his family waited at a checkpoint.

Fayyaz Khan, a Pakistani official at Torkham, said large numbers of Afghans were returning home, along with smaller groups of Pakistanis, but that overland trade between the two countries has yet to resume.

Pakistan shut the crossings three weeks ago after a wave of suicide bombings that authorities said was linked to Islamic militants based in Afghanistan. The two countries have long accused each other of ignoring al-Qaida and other militants who operate along the porous, mountainous border.

Khairullah Azad, the deputy spokesman for the Afghan Foreign Ministry, urged Pakistan to restore normal movement at the crossings, saying the two-day reopening was insufficient for people living on both sides of the frontier.

The opening of the crossings came a day after Pakistan said a group of militants crossed over from Afghanistan and attacked several military posts, setting off clashes that killed six soldiers and 10 militants. It said it had complained about the attack to Kabul and called on Afghanistan to take action.

Meanwhile, at least two Pakistani soldiers and five militants were killed Tuesday in the northwestern town of Swabi during a raid on a militant hideout, the military said. It provided no further details, saying the fighting was still underway. The Pakistani Taliban claimed it attacked soldiers in Swabi.

Prime Minister Nawaz Sharif expressed his condolences over the “martyrdom” of soldiers and praised the armed forces for battling those who threaten the state.