ISTANBUL — Turkey said on Sunday that it was launching a major counteroffensive against the Syrian government, for the first time openly declaring war on the government of Bashar al-Assad and steeply escalating Ankara’s involvement in Syria’s nine-year war.

The announcement came three days after Turkey suffered its worst military losses in a single attack of the war, with at least 36 soldiers killed and more than 30 wounded in Syrian and Russian air and artillery strikes. Turkey’s president, Recep Tayyip Erdogan, vowed retaliation and demanded that Syrian troops withdraw beyond a previously agreed upon de-escalation boundary.

Turkey’s defense minister, Hulusi Akar, announced the counteroffensive on television in a statement distributed by the state-run Anadolu news agency, saying that the operation in northern Syria was intended to end the war and stop a Syrian government offensive that was displacing thousands of Syrians.

“All of our efforts are primarily to ensure a cease-fire, to prevent migration and to stop the flow of blood,” Mr. Akar said, “and thus to bring peace, peace and stability to the region as soon as possible.”