People threw punches at Republican People's Party leader Kemal Kilicdaroglu as security officials tried to escort him away from the funeral in a village outside of Ankara, television footage showed. An angry crowd then surrounded a house where he was taken for safety.
Kilicdaroglu was later driven out of the village in an armored vehicle.
The soldier was among four killed Saturday in a clash against the rebels of the outlawed Kurdistan Workers' Party rebels, or PKK, near Turkey's border with Iraq. The group is considered a terror organization by Turkey, the United States and the European Union.
The Ankara governor's office said legal action had been launched against the assailants.
Kilicdaroglu's pro-secular Republican People's Party, or CHP, won control of Ankara and Istanbul during local elections on March 31. Turkish President Recep Tayyip Erdogan's ruling party previously governed in the two cities.
Erdogan led the Justice and Development party's divisive election campaign, portraying the local races as a matter of national survival and equating opposition parties with terrorists.
The funeral assault cast a shadow on a rally organized in Istanbul to celebrate the CHP mayoral candidate's win, which ended the ruling party's and its Islamist predecessor's 25-year hold on the city of 15 million.
The ruling party is now seeking a rerun of the election, claiming widespread irregularities. Turkey's top electoral body is considering the ruling party's appeal.
Ekrem Imamoglu, 48, was confirmed as mayor on Wednesday following two weeks of appeals and recounts requested by the ruling party. He has taken a conciliatory tone, promising to bring people together following the polarizing elections.
Copyright 2019 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.