From mayor to lawmaker and prime minister to president, Recep Tayyip Erdogan rose through the ranks to Turkey’s highest positions and then made them his own, bringing the country over the course of 20 years closer to one-man rule.

On Sunday, Mr. Erdogan will try to secure another term as president, although only after the opposition forced him into a runoff vote. That the election has gone to a second round is a sign that his grip on the country has slipped, if not been broken, amid a host of problems like economic turmoil, widespread corruption and his government’s handling of catastrophic earthquakes this spring.

But Mr. Erdogan has navigated crises since the earliest days of his career, including a jail sentence, mass protests and an attempted coup. Several of those episodes illustrate how he not just survived crises, but found opportunities to consolidate power through them.

In 1998, Mr. Erdogan, then Istanbul’s 44-year-old mayor, was a rising star of Turkey’s Islamist political movement — which was the target of a crackdown by the military-backed authorities. That year, a court convicted him of having called for religious insurrection by quoting an Islamist poem from the 1920s. He was sentenced to 10 months in jail and handed a lifetime ban on political activity.

Although predominantly Muslim, Turkey was founded as a secular republic and the traditional political elites felt the Islamists were anathema to those values.

Mr. Erdogan spent four months in jail, making plans for a comeback despite the ban. In a general amnesty in 2001, Turkey’s Constitutional Court lifted the ban, and he soon assembled a new political party with other reformists from the Islamist movement who promised good governance and sought ties with the West.

Mr. Erdogan’s ascent was nearly stopped in 2002 by Turkey’s electoral board, which barred him from an election because of his criminal conviction. But his party colleagues, who had swept into Parliament, amended the Constitution to let him run. Mr. Erdogan won office and became prime minister in 2003.

His government also began prosecuting some of those figures, in 2008 accusing dozens of people, including retired army generals and journalists, of trying to stage a coup. Mr. Erdogan’s allies called the trial an attempt to reckon with Turkey’s history of violent power struggle. Critics called it an effort to silence the secular opposition.

With voters’ approval in a referendum two years later, Mr. Erdogan reshaped the Constitution again. He said the 2010 overhaul brought Turkey closer to Europe’s democracies and broke from its military past, while his opponents said it gave his conservative government greater control over the military and the courts. He won a third term as prime minister in 2011.

Mr. Erdogan was not without significant, if disparate, opposition. In 2013, protests that erupted over a proposed mall to replace an Istanbul park morphed into a demonstration of discontent over many issues, including the drift toward Islamist policies and persistent corruption.

Mr. Erdogan cracked down, not just on protesters but also on medics, journalists, activists, business owners and officials accused of sympathizing. Some cultural figures were imprisoned and others fled, and for many who remained, an atmosphere of self-censorship descended.

As his term neared its end, Mr. Erdogan faced a problem: His party’s rules prevented him from another turn as prime minister. In 2014, he instead ran for another office — becoming Turkey’s first popularly elected president, opening his term with words of rapprochement.

“I want us to build a new future with an understanding of societal reconciliation, while considering our differences as our richnesses and bringing forward our common values,” he said in a victory speech.

But rather than limit himself to the mostly ceremonial duties of the role, he moved to maximize its powers, which included a veto on legislation and the ability to appoint judges.

Mr. Erdogan’s rule nearly ended in 2016, as a chaotic insurrection by parts of the military and members of an Islamist group that had once been his political ally tried to oust him. But he skirted capture, called Turks to protest in the streets and soon re-emerged in Istanbul to reassert control.

“What is being perpetrated is a rebellion,” he said. “They will pay a heavy price for their treason to Turkey.”

A purge that followed reshaped Turkey: Thousands accused of connections to the coup plot were arrested, tens of thousands lost jobs in schools, police departments and other institutions, and more than 100 media outlets were shuttered. Most of those caught up in the purge were accused of affiliations with the Gulen movement, the Islamist followers of Fethullah Gulen, the cleric accused by Mr. Erdogan of orchestrating the coup while living in exile in the United States.

Within a year, Mr. Erdogan had arranged another referendum for voters, this one on whether to abolish the post of prime minister and move power to the president, as well as grant the role more abilities.

With his opponents under pressure and his allies reinvigorated, he narrowly won the referendum, calling the changes necessary to make the government more efficient. The next year, he won re-election to another five-year term.

Hours before his inauguration in 2018, Mr. Erdogan published a 143-page decree that changed the way almost every government department operated. He fired another 18,000 state employees and made several major appointments, naming his son-in-law the new finance minister.

The decree was just one sign of how far Mr. Erdogan has taken Turkey down the path toward strongman rule. The government announced new internet restrictions and started monumental projects — including soaring bridges, an enormous mosque and a plan for an “Istanbul Canal.”

Many of Mr. Erdogan’s supporters hail efforts like these as visionary, but critics say they feed a construction industry that is plagued by corruption and which has wasted state funds.

Those frustrations have spread among many Turks in recent years. While Mr. Erdogan has raised Turkey’s stature abroad and pursued major projects, his consolidation of power has left some uneasy, and the economy has suffered.

That dissent has loosened Mr. Erdogan’s hold over the country.

In 2019, his party lost control of some of Turkey’s largest cities — only to contest the results in Istanbul. Turkey’s High Election Council ordered a do-over election, a decision condemned by the opposition as a capitulation to Mr. Erdogan, but his party lost that second vote, too, ending 25 years of dominance in Turkey’s largest city.

And now, with his government criticized for its preparation for earthquakes and its response to them, and Turkey’s economy teetering on the verge of crisis, Mr. Erdogan has persisted with major spending and lowering interest rates despite inflation, which has left many Turks feeling far poorer.