Spanish premier Pedro Sanchez's pledge came a day after the United Nations denounced authorities on the border between Morocco and Spain for using "excessive force", describing it as "unacceptable."
"I regret the loss of human life and express my solidarity with the families of the migrants who died," Sanchez told Cadena Ser radio, pledging his government would work with investigators to understand what happened.
Sanchez stressed that three investigations had been opened, one by Moroccan prosecutors, one by Spain's public prosecutor and a third by the Spanish rights ombudsman.
"We have to trust these institutions and I pledge the government's total collaboration with their efforts to clarify what happened," he said.
Spain's public prosecutor on Tuesday opened its own investigation "to clarify what happened", citing the "seriousness and gravity" of the incident.
In Morocco, prosecutors are pressing charges against 65 migrants, mostly Sudanese, for trying to storm the border.
The tragedy happened at dawn on June 24, when some 2,000 migrants, mostly from sub-Saharan Africa, tried to break through the fence from Morocco into Melilla, one of Spain's two tiny North African enclaves.
Moroccan authorities said some of the victims had fallen while trying to scramble over the fence, giving an initial toll of 18 dead, but later raising it to 23 after another five migrants died of their injuries.
Few details about the incident were available, but Spanish media showed footage of people on the ground, some with bloodied hands and torn clothes.
The death toll was by far the worst recorded in years of attempts by migrants to cross into Spain's Ceuta and Melilla enclaves, which have the EU's only land borders with Africa, making them a magnet for those desperate to escape grinding poverty and hunger.