ISTANBUL — A last-ditch effort by President Recep Tayyip Erdogan’s governing party to annul the election for mayor of Istanbul has opened wide divisions in the party’s rank and file and with its nationalist allies, as even the president has come under unusual attack.

These tensions, including an ugly physical assault on an opposition lawmaker, have underscored just how severe a blow the loss of Istanbul, still to be officially confirmed, has inflicted on Mr. Erdogan’s once seemingly solid power structure.

For the first time in 25 years, since Mr. Erdogan first won power as mayor of Istanbul in 1994, his Justice and Development Party, or A.K.P., has lost control of his home city and power base, along with four other major cities, including the capital, Ankara.

[Update: Turkey orders new election for Istanbul mayor, in setback for opposition.]

The party has mounted an extraordinary appeal to have the Istanbul election canceled and a new election held. Yet it remains deeply divided about the best way forward.