Around 10,000 Afghans have taken to the streets of Kabul to show their anger and frustration over the brutal killings of seven members of a Shiite minority.
Protesters also called on the government to do more to ensure the nation's security or step down.
The case of seven Hazaras, whose beheaded bodies were found in the country's south-eastern province of Zabul on Saturday, appears to have galvanised many in war-battered Afghanistan, uniting them across ethnic and sectarian divides.
No one claimed responsibility for the killings, although Afghan officials have blamed a range of extremists, from Taliban insurgents to an emerging affiliate in Afghanistan of Islamic State, which is fighting in Syria and Iraq.
The protest came to a head outside President Ashraf Ghani's heavily fortified palace at Pashtunistan Square, where marchers brought the coffins, draped in green flags symbolising the Shiite minority.
At one point, presidential guards opened fire as some of the protesters tried to scale the walls and get into the palace grounds.
The president's deputy spokesman said the shooting wounded 10 people. It was not clear whether Mr Ghani was inside the palace at the time.
Shortly afterwards, Mr Ghani went live on national television, appealing for calm and promising that the Hazara deaths would be avenged.
"The nation's pain is my pain," he said, vowing the authorities would have "no mercy" on the killers.
"Like you, I will not calm down until the perpetrators of these crimes are brought to justice. We shall revenge the blood of our brothers and sisters," he said, adding that the "enemies of Afghanistan" are trying to create disunity and "bring ethnic and sectarian violence" to the country.
After the gunshots, many of the protesters dispersed but around 1,000 were allowed into the palace compound.
About 30 of them were to meet with Mr Ghani and members of his National Security Council later on Wednesday to press demands for justice and security, according to organiser Lilia Mohammadi.
The seven coffins had also been moved inside the palace grounds, she said, where they would stay the night - a conciliatory gesture following the gunfire.
Kabul's deputy police chief Gul Agha Rouhani said security forces shot in the air to disperse the protesters trying to scale the palace walls. He could not confirm reports of injuries.
Later, riot police took up positions at the gates as a tense calm returned.
"About 100 people were trying to get into the palace by climbing over the wall," said Ahmad Sharif, 36, who took part in the rally. He said he saw one protester hit by gunshots.
Before arriving at the palace gates, the protesters walked for almost four hours carrying the green-draped coffins of the seven Hazaras - four men, two women and a nine-year-old girl.
The seven were seized in Ghazni province, which borders Zabul, up to six months ago.
Afghanistan's spy agency dismissed Taliban claims that affiliates of Islamic State were behind the killings.
In the past five days, rival Taliban groups have been fighting each other in the area where the bodies were found.
The United Nations Assistance Mission in Afghanistan (Unama) condemned the killings and said they may constitute a war crime.
Killing and kidnapping civilians are "serious violations of international humanitarian law", Unama chief Nicholas Haysom said and called for the perpetrators to be held accountable.
During the rally, the Kabul marchers called on Mr Ghani and his chief executive Abdullah Abdullah to resign if they cannot guarantee the security of the country.
"We came to the front gate of palace to raise our voices for justice and to condemn all acts of murder in the country," said Sakhi Rizayee 37, one of the organisers of the protest.
The protesters included members of all Afghanistan's ethnic groups, according to activist Zahra Sepehr, who said the turnout was about 10,000.
That would make it the biggest demonstration in Kabul since the March killing of a young woman, Farkhunda Malikzada, by a mob enraged over false reports that she had burned a copy of the Quran.
Ms Sepehr said: "We want justice and we want this government, Ghani and Abdullah, to go so that we can have a government that protects all the people of the country and brings security to the whole country."
Another activist and protester in Kabul said Wednesday's demonstration aimed to show the unity of the Afghan people, regardless of ethnicity or sect.
He said: "Our voice wants justice, brotherhood, sanctity of life, peace and equality. No one can tolerate any longer the situation we have in the country."