"These have been days of tears, many tears but also of much humanity," Barcelona's Auxiliary Bishop Sebastia Taltavull said as many mourners listened with their heads bowed.
Spain's King Felipe and his wife Letizia, who wore black, along with Prime Minister Mariano Rajoy and Catalonia's president Carles Puigdemont, led the ceremony attended by more than 1,800 people.
They were joined by Portugal's Prime Minister Antonio Costa and President Marcelo Rebelo de Sousa for a mass broadcast live on Spanish television.
A 74-year-old Portuguese woman and her 20-year-old granddaughter, were among the 13 people killed when a white van ploughed into crowds of pedestrians on Barcelona's Las Ramblas boulevard on Thursday.
The pair had just checked into their hotel and were going for a walk when they were struck by the vehicle.
Hours later, a similar assault took place in the seaside town of Cambrils further south along the coast, leaving one woman dead after one of the assailants stabbed her with a knife.
The Islamic State group has claimed both attacks.
The Archbishop of Barcelona, Cardinal Joan Josep Omella, read a message of condolences sent by Pope Francis, who called the attacks a "cruel terrorist act" and a "grave offence to God".
'Moments of pain for our city'
In his homily the cardinal gave thanks for all prayers and shows of concern "in these moments of pain for our city, for the families who have suffered the blow of the attack and for all good people."
"Is it not a serious sin to take the life of one's peers, of innocent people, or children? Our silence, our prayers, our presence in this sacred place is a sign of our condemnation of the attack," he added.
Many people at the mass wiped back tears or fanned themselves against the heat below the church's forest of white tree-like columns that rise 60 metres (200 feet) up and split into branches.
'Could have happened to me'
The service was open to the public and it drew many tourists, many dressed in shorts and T-shirts, as well as locals.
Catalonia resident Teresa Rodriguez said she had turned up to pray for the victims who came from three dozen countries including those as far afield as Australia, China and Peru.
"What happened in Las Ramblas is really hard for us, we go for walks there often, it could have happened to me, my children or anyone. And here we are. It's huge, huge," she said as she fought back tears.
Francisca Rubio, a tourist from Chile, said she had come to pray that "what happened in Barcelona does not happen anywhere else".
"We are many who don't want these things to keep happening, not here or anywhere."
While one row of the church was filled with uniformed police, representatives of Barcelona's Muslim community were also in attendance.
"It was really important for me to be here," said Marta Laura Pena, a visitor from Mexico who came to the mass on her last day in Barcelona.
"I am Catholic and I am worried about the situation of the world and I am very saddened that there was an attack here in Barcelona."
Snipers were posted on rooftops surrounding the landmark church by visionary architect Antoni Gaudi whose eight spires are an emblematic fixture of Barcelona's skyline.
Heavily armed police stood guard as hundreds of people gathered outside the unfinished Art Nouveau building under grey skies.
The Sagrada Familia had its first stone laid in 1882 but work is not scheduled to finish for many more years.
When completed, Gaudi's elaborate design will feature 18 towers dedicated to the 12 apostles, four evangelists, Jesus and the Virgin Mary.
By Benjamin Bouly Rames / AFP