With quick and coordinated action by some of its top doctors, Nigeria, Africa’s most populous country, appears to have contained its first Ebola outbreak, the United States Centers for Disease Control and Prevention said Tuesday.

As the epidemic rages out of control in three nations only a few hundred miles away, Nigeria is the only country to have beaten back an outbreak with the potential to harm many victims in a city with vast, teeming slums.

“For those who say it’s hopeless, this is an antidote — you can control Ebola,” said Dr. Thomas R. Frieden, director of the C.D.C.

Although officials are pleased that success was achieved in a country of 177 million that is a major transport and business hub — and whose largest city, Lagos, has 21 million people — the lessons here are not easily applicable to the countries at the epicenter: Guinea, Liberia and Sierra Leone. Public health officials in those countries remain overwhelmed by the scale of the outbreak and are desperate for additional international assistance.

Nigeria’s outbreak grew from a single airport case, while in the three other countries the disease smoldered for months in remote rain-forest provinces and spread widely before a serious response was mounted.

Ebola, Dr. Frieden said, “won’t blow over — you have to make a rapid, intense effort.”

While the danger in Nigeria is not over, the health minister, Dr. Onyebuchi Chukwu, said in a telephone interview that his country was now better prepared, with six laboratories able to make diagnoses and response teams and isolation wards ready in every major state.

After the first patient — a dying Liberian-American — flew into Lagos on July 20, Ebola spread to 20 other people there and in a smaller city, Port Harcourt.

They have all now died or recovered, and the cure rate — 60 percent — was unusually high for an African outbreak.

Meanwhile, local health workers paid 18,500 face-to-face visits to repeatedly take the temperatures of nearly 900 people who had contact with them. The last confirmed case was detected on Aug. 31, and virtually all contacts have passed the 21-day incubation period without falling ill.

The success was in part the result of an emergency command center financed in 2012 by the Bill & Melinda Gates Foundation to fight polio. As soon as the outbreak began, it was turned into the Ebola Emergency Operations Center.

Also, the C.D.C. had 10 experts in Nigeria working on polio and H.I.V., who had already trained 100 local doctors in epidemiology; 40 of them were immediately reassigned to Ebola and oversaw the contact tracing.

The chief of the command center, Dr. Faisal Shuaib, gave credit to a coordinated effort by the Health Ministry, the C.D.C., the World Health Organization, Unicef, Doctors Without Borders and the International Committee for the Red Cross.

Also, he noted, Nigeria has significant advantages over poorer countries where the outbreak is out of control.

It has many more doctors per capita, some educated abroad at top medical schools.

It has standing teams of medical investigators, with vehicles and telephones, who normally trace outbreaks of other ills like cholera or Lassa fever.

Lagos University Teaching Hospital was able to do Ebola tests in six hours.

The hospitals where patients were isolated were equipped to do tests for electrolytes and blood proteins, both of which must be kept in balance as patients are fed orally or intravenously to replace fluids lost to diarrhea and vomiting.

And air-conditioned hospitals let people wearing protective gear work longer without overheating.

Nigeria also had some luck. Although the first patient, a businessman named Patrick Sawyer, was vomiting on his flight in, none of the roughly 200 others on the plane fell ill. Others did after helping him into a taxi to a hospital.

And a patient in Port Harcourt went to her church and became violently ill during a ceremony in which the congregation laid hands on her. But none became infected.

Dr. Sue Desmond-Hellmann, the Gates Foundation’s chief executive, said she was “heartened to see this positive result of the efforts of so many in Nigeria.”

On July 17, Mr. Sawyer defied medical advice and left a hospital in Monrovia, Liberia, where he was being held for observation after caring for his sister, who died of Ebola, although it was unclear whether he knew what she had.

Nigerian news reports said he used Liberian government contacts for permission to leave, flying to Lagos by way of Ghana and Togo. He planned to go to an economic development conference there and then fly back to Coon Rapids, Minn., for his children’s birthdays, according to media interviews with his widow.

Taken to a small private hospital after he collapsed, he denied any contact with Ebola victims and was initially treated for malaria. He died on July 25.

“That hospital had zero infection control,” Dr. Frieden said.

A nurse who helped reinsert an IV line when Mr. Sawyer was delirious and bleeding wore no gloves, had a cut on her hand and did not wash it, he said. She later died.

After malaria treatment failed, Ebola was “high on the index of suspicion,” Dr. Shuaib said.

He learned about Mr. Sawyer’s diagnosis as he sat chatting in his office with a colleague.

“I thought: ‘Oh, my God, not Nigeria. Not Lagos.’ I knew the potential for it to spread in a densely populated place.”

Even though the emergency center swung into action quickly and aggressive contact tracing was possible because Nigeria’s Port Health Services obtained records of Mr. Sawyer’s travel, there were still problems.

It took 14 days, Dr. Frieden said, for the first isolation ward to open in a former tuberculosis ward.

“Health workers initially wouldn’t go in,” he said. “They were afraid. We ultimately trained 1,800 staff.”

Wards were reconfigured to add space between beds, put in washing stations with chlorinated water and create rooms where doctors and nurses could carefully don and remove protective gear. The worked in teams of two so they could watch each other and prevent mistakes.

Also, according to a C.D.C. study released Tuesday in the Morbidity and Mortality Weekly Report, inaccurate news media reports before the government began offering official information “created a nationwide scare.”

Sales of false cures, including “Blessed Salt,” shot up, and two Nigerians died of drinking large amounts of saltwater.

But Dr. Shuaib emphasized that even terrified Nigerians did not deny the virus’s existence or attack health workers, as happened in the other countries. “No conspiracy theories entered the debate,” he said.

Nigeria’s success shows how important preparation is, said Dr. Frieden, adding, “Some countries that could well be the next Lagos still don’t have a clue about how to deal with this.”