For his part, King Juan Carlos helped a woman soldier lay a medal on the coffin of her dead husband.
Some mourners were so overcome with grief at the service at Spanish army headquarters in Madrid that they had to leave parts of the ceremony.
The king and other members of the royal family led mourners, who included Prime Minister Jose Luis Rodriguez Zapatero, members of his cabinet and other high-ranking politicians as well as the families of the 17 dead soldiers.
Monsignor Manuel Monteiro de Castro, papal envoy to Spain, read out a message of sympathy from Pope Benedict XVI, who was in Germany.
Archbishop Francisco Perez Gonzalez said, "The military is at the service of safety, freedom and peace. When life is offered for big ideals, when it is based on solid human and Christian principles, all its energy is at the service of the common good."
A poignant moment came when the king assisted Sergeant Susana Perez Torres in laying a medal on the coffin of her husband, Sergeant Alfredo Francisco Joga.
Representatives of the US, Britain, France and Nato were present. Spanish state television TVE1 read out condolences sent to the Spanish government by US President George W Bush.
The crash on Tuesday was Nato's largest single loss of life in Afghanistan.
Tuesday's crash was Nato's single
biggest loss life in Afghanistan
A second Spanish helicopter flying alongside at the time of the accident was forced to make an emergency landing, injuring five soldiers on board.
The Ministry of Defence said on Saturday that four of them had been released from hospital
The Spanish soldiers were part of the Nato-led security force preparing for next month's parliamentary elections in Afghanistan.
Investigators have so far found no evidence that the helicopter was downed by hostile fire, favouring instead the theory that a strong gust of wind may have forced it down while it flew at high speed close to the ground.