The Playa de Bakio was attacked 400km off the Somali coast
Pirates have released a Spanish fishing boat and its 26 crew members seized off the coast of Somalia last week.
The Playa de Bakio, a Basque tuna fishing boat, was released along with all of its crew on Saturday, the Spanish government says.
Deputy Prime Minister Maria Teresa de la Vega said the crew were safe and on their way to "safer waters".
She said the release had been achieved through talks. A local official told the BBC a $1.2m ransom had been paid.
"Five men on a speed boat came to the ship and informed the pirates that they were ready to meet their demand of ransom," said Abdi-salam Khalif Ahmed, a local leader in the Somali port town of Haradhere.
"They then boarded the ship and handed a bag with $1.2m to the leader of the pirates."
Ms de la Vega, who did not comment on whether a ransom had been paid, said her government had taken steps to ensure that "a similar situation does not taken place again".
"We must put an end to maritime piracy," she added.
The Basque tuna boat was attacked by pirates armed with grenade launchers about 250 miles (400km) off the coast of Somalia on 20 April.
The government was making plans to repatriate the crew of 13 Spaniards and 13 Africans, Ms de la Vega said.
Somali coastal waters are among the most hazardous in the world.
Last year, more than 25 ships were seized by pirates in Somali coastal waters.
Somalia has not had an effective central government for more than 17 years and is plagued by insecurity.