GAZIANTEP, Turkey — A powerful earthquake struck northwestern Turkey in 1999, killing more than 17,000 people, exposing government incompetence and fueling an economic crisis. Amid the turmoil, a young, charismatic politician rode a wave of public anger to become prime minister in 2003.

That politician was Recep Tayyip Erdogan.

Now, as president, Mr. Erdogan faces challenges similar to those that brought down his predecessors — posing what is perhaps the greatest threat of his two decades in power to his political future.

The deadliest earthquake to strike Turkey in almost a century killed at least 20,000 people this past week, with the bodies of countless others still buried in the rubble. It hit after a year of persistently high inflation that has impoverished Turkish families, leaving many with scarce resources to bounce back.

The quake’s aftermath has highlighted how much Mr. Erdogan has reshaped the Turkish state, analysts said. Critics accuse him of pushing the country toward autocracy by weakening civil rights and eroding the independence of state institutions, like the Foreign Ministry and the central bank. And in a series of moves aimed at undercutting his rivals and centralizing control, he has restricted institutions like the army that could have helped with the earthquake response while stocking others with loyalists.

Mr. Erdogan acknowledged on Friday that his government’s initial response to the disaster had been slow, and anger was building among some survivors, a sentiment that could hamper his bid to remain in power in elections expected on May 14.

“I have been voting for this government for 20 years, and I’m telling everyone about my anger,” said Mikail Gul, 53, who lost five family members in a building collapse. “I will never forgive them.”

The president, who faced harsh criticism in 2021 over his government’s failure to control disastrous wildfires, has long portrayed himself as a leader in touch with the common citizen. He visited communities hit hard by the quake in recent days. Dressed in black, his face grim, he visited the wounded and comforted people who had lost their homes and emphasized the magnitude of the crisis.

“We are face to face with one of the greatest disasters in our history,” he said on Friday during a visit to Adiyaman Province. “It is a reality that we could not intervene as fast as we wished.”

The 7.8 magnitude earthquake — the most powerful in Turkey in decades — and hundreds of aftershocks toppled buildings along a 250-mile-long swath in the south, destroying thousands of buildings and causing billions of dollars in damage. Across the border in Syria, nearly 4,000 dead have been counted, a toll that is expected to rise significantly.

“This is the largest-scale disaster that Turkey has to manage, and, inevitably, this will create a backlash against the government,” said Sinan Ulgen, the director of Edam, an Istanbul-based think tank. “But much will depend on how effectively it can address the needs of the affected population.”

The Turkish government has begun an extensive aid operation, dispatching 141,000 aid and rescue workers to search for the dead and wounded, to distribute food, blankets and diapers and to erect tents for the tens of thousands of homeless, many of them sleeping in cars to avoid the subzero winter chill.

Nevertheless, many survivors have expressed frustration with the government’s response, saying the state was nowhere to be found during the initial aftermath, leaving residents alone to find shelter and free trapped loved ones from collapsed buildings.

The scarcity of trained rescue squads and heavy machinery during the critical first days most likely increased the death toll because many people who could have been saved were not.

When government agencies arrived, residents said, their equipment seemed insufficient and they failed to coordinate the efforts of volunteers who were already struggling to help survivors.

For two days after the quake, Mr. Gul said his family lacked food and water and felt helpless amid the destruction.

“The house next to us collapsed and there was a girl inside saying, ‘Save me! save me!’” he said.

The girl was saved, but Mr. Gul and his relatives had to dig out their five dead family members, he said.

He had worked in Germany for 20 years, funneling his savings into 10 apartments in the city of Kahramanmaras, near the quake’s epicenter, so he could live off the rent. But all of the apartments were destroyed, and he has to start over.

“I don’t know what I’m going to do,” he said.

During his two decades as prime minister and president, Mr. Erdogan has argued that changes to the way Turkey was run were necessary to protect it from a range of domestic and foreign threats, including military coups and terrorist groups.

He has also restricted the army, which played a key role in the government’s response to the 1999 earthquake.

Turker Erturk, a former Navy admiral who was a commander in the crisis center set up after that quake, said in an interview that the army had swiftly intervened. But in the years since, Mr. Erdogan’s government had limited that ability and the army had stopped planning and training for it, he said.

After Monday’s quake, the government called on the army only after public criticism, according to Mr. Erturk.

“It is because of one-man rule,” he said. “In authoritarian governments, those decisions are made at the very top, and they wait for his commands.”

On Friday, the army said in a tweet that its soldiers had been helping “from the first day” and now had more than 25,000 soldiers deployed. But their presence has not been obvious in many of the hardest-hit areas.

Leading the government’s earthquake response is the Disaster and Emergency Management Presidency, or AFAD, which critics say Mr. Erdogan has stocked with loyalists and empowered at the expense of other organizations, like the Turkish Red Crescent.

The earthquake has also led to increased scrutiny of the government’s use of construction codes aimed at preventing buildings from collapsing, according to analysts

Although no one can predict the precise timing of an earthquake, seismologists have been warning for years that a big one was expected in this region.

Three days before the quake, a prominent geologist, Naci Gorur, wrote on Twitter that he was concerned that other seismic activity in Turkey had put pressure on the faults near the epicenter of Monday’s tremor. He even posted a map pinning some of the locations that would be the hardest hit if his predictions came to pass.

After the quake, he tweeted again, saying: “As geologists, we grew exhausted of repeating that this earthquake was coming. No one even cared what we were saying.”

Following the 1999 quake, Turkey strengthened its construction codes to make buildings more earthquake resistant.

But the zone devastated by the recent quakes is dotted with areas where some buildings survived while others nearby — some relatively new — completely collapsed, raising questions about whether some contractors had cut corners.

At one collapsed apartment block this week, volunteer construction workers spotted what they said was inferior rebar and they broke up chunks of concrete with their hands, saying it was poor quality.

In the days since, a lawyers’ association has asked prosecutors in Kahramanmaras to identify contractors who built buildings that collapsed and inspectors who checked them so they can be investigated for possible criminal violations. Prosectors in Gaziantep have started collecting rubble samples for their own investigation.

The earthquake left behind billions of dollars in damage, and government plans will require billions more at a time when the state budget is already strained.

Before the quake, Mr. Erdogan’s government unleashed billions of dollars in new spending aimed at cushioning the blow of high inflation to citizens before the election, a cash injection that some economists predicted could tip the country into recession this year.

On top of economic hardship, the earthquake will deepen Turks’ distress, and not in a way that makes them feel that they are contributing to a greater cause, said Selim Koru, an analyst at the Economic Policy Research Foundation of Turkey.

“This, by its nature, comes out of nowhere, and it makes people even more miserable, and not just in the earthquake zone,” he said. “The economy is going to suffer, and I’m not sure it gives that suffering any meaning.”

The earthquake’s proximity to the presidential and parliamentary elections that must be held on or before June 18 could lead to other challenges.

The Reuters news agency quoted an unnamed Turkish official on Thursday as saying the earthquake’s devastation posed “serious difficulties” for the vote. It was the first hint that the government could seek to postpone it.

Trying to unseat Mr. Erdogan is a coalition of six opposition parties that want to bolster the economy and restore independence to state institutions. They have already started trying to turn the quake response into an election issue.

But even some angry voters still trust Mr. Erdogan.

“We failed this test,” said Ismail Ozaslan, 58, a long-haul truck driver in a park in Gaziantep where part of his family was cramped inside a tent. “We are like patients left to die. There is no management here.”

But his criticism of local and national officials, whom he accused of corruption and neglect, stopped short of Mr. Erdogan.

“It’s like a building where the roof is strong but the pillars are rotten,” he said. “We don’t have a chance other than Erdogan. May God grant him a long life.”

Safak Timur contributed reporting from Gaziantep, and Gulsin Harman from Istanbul.