The troops were among security forces deployed to search for mine workers, including four Chinese nationals, said to have been kidnapped during the attack on Wednesday.
Emmanuel Umar, the commissioner for state security in northwestern Niger, initially said an unknown number of people had been killed during the mine attack in Ajata Aboki village.
When the security forces responded to the attack, the gunmen ambushed them and opened fire, killing 30 soldiers travelling in three trucks, two army sources in Shiroro and Minna, the Niger state capital, said on Saturday.
"It's heartbreaking to lose our men who went for reinforcements. It weakened us but we shall not relent," said the soldier from an army base in Shiroro, who cannot be named because he is not authorised to speak to the press.
The Nigerian army, which rarely comments on losses in battle, acknowledged that "a number of personnel paid the supreme price in fierce firefight that ensued".
A local leader in Shiroro told Reuters by phone that the gunmen had arrived on motorbikes and a truck at the mine before opening fire, killing seven policemen on guard.
They abducted the Chinese workers and shot dead eight civilians, he added. It was not immediately clear if the civilians were workers from the mine.
A senior officer from Nigeria's Training and Doctrine Command, in Minna, is leading an operation to locate the kidnapped workers, the leader said.
A Niger state police spokesperson declined to comment.
President Muhammadu Buhari said the gunmen were "sadists" who would be found and punished. "Shiroro will see justice," he said in a Twitter post on Saturday.
It was not immediately clear who carried out the attack in Niger, where gunmen are known to carry out attacks on villages. Local officials said last year that suspected Boko Haram militants had established a presence in Shiroro.