DAKAR, Senegal — The political standoff in Gambia intensified on Thursday as foreign troops crossed the border with orders to dislodge a repressive leader who has refused to step down after losing a presidential election last month.

Gambia’s erratic leader, Yahya Jammeh, seized power in a coup 22 years ago and once said he could rule for a billion years. But on Thursday the Senegalese military headed toward the capital of Gambia, Banjul, where Mr. Jammeh has been holed up in the state house, insisting that his rule is still valid.

Mr. Jammeh has warned that he will fight back against any foreign military intervention. At least 26,000 Gambians, worried about violence, have fled the country, the United Nations says, and several senior officials in Mr. Jammeh’s government have resigned in protest or have left the nation as well.

As the invasion began, the winner of the election, Adama Barrow, was being sworn into office in a brief ceremony about 150 miles outside of Gambia. The inauguration took place in a nondescript room at the Gambian Embassy in Dakar, Senegal, because Mr. Barrow has so little control over his country that he did not go home for the funeral of his son, who had been killed by a dog over the weekend.

Mr. Barrow’s team ultimately decided that the embassy in Dakar was the closest they could safely get to Gambian soil to start the new administration. Plans for an elaborate ceremony in a soccer stadium in Gambia’s capital had to be scrapped amid the standoff.

“It’s not necessarily the kind of ceremony to be jubilant about, but the situation is dangerous,” said Halifa Sallah, a spokesman for Mr. Barrow.

But late on Thursday it appeared the situation could still be resolved without a fight. State television broadcast news that the presidents of Liberia, Mauritania and Guinea would arrive Friday to negotiate a resolution. A Senegalese military official said troops would stand by for talks.

During the inauguration in Senegal, Mr. Barrow arrived to enthusiastic whoops from hundreds of supporters and emerged from a motorcade beaming. He was sworn in just before 5 p.m., his hand on a Quran, pledging to serve his country “without fear or favor.” Several people who had gathered outside to watch on a large television screen shouted, “No more dictatorship!”

In a brief speech, the soft-spoken Mr. Barrow called on the Gambian security forces to pledge their loyalty to him, asking that the military stay in their barracks.

“We are one Gambia, one people,” he said.

Gambia has suffered high unemployment and political repression for years under Mr. Jammeh, with many citizens abandoning the country to risk perilous and often deadly journeys to Europe by sea.

Minutes after Mr. Barrow was sworn in, celebrations erupted across Gambia, with thousands gathering in some areas.

Lamin Demba, a welder, said he felt like a free man. Joining others in celebration in front of his welding shop with piles of metal, he took out his phone and called a relative in Italy.

“Come back home boy, we are free now,” he shouted into the phone. “Now Gambia is free.”

But while Mr. Barrow has popular support in Gambia, it has not been enough to dislodge the erratic Mr. Jammeh. After initially accepting defeat in the election, he suddenly cited voting irregularities and called for a new vote.

Video

Adama Barrow, the newly elected president of Gambia, was sworn in on Thursday in a ceremony at an embassy in neighboring Senegal. Yahya Jammeh, Gambia's repressive leader for the past 22 years, has refused to step down after losing the recent presidential election.Published OnJan. 19, 2017CreditImage by Senegalese Presidency

“He is still the president,” said Seedy Njie, Gambia’s minister of information, communication and technology. “So, yes, he is here.”

This week, Gambia’s Parliament voted to extend Mr. Jammeh’s term for three months, though international officials denounced the move and many analysts consider it unconstitutional.

The United Nations Security Council unanimously adopted a resolution on Thursday supporting the efforts of the Economic Community of West African States to ensure a peaceful transfer of power to Mr. Barrow.

The resolution, presented by Senegal, which has a rotating seat on the council, offers “its full support to the Ecowas in its commitment to ensure by political means first the respect of the will of the people” of Gambia in the election that ousted Mr. Jammeh from office.

The speed with which the council has taken action shows how little support Mr. Jammeh has among world powers. He has overseen a legacy of human rights abuses, including imprisoning opponents and declaring that gay people should be beheaded. He has claimed to have the ability to cure AIDS with little more than an herbal concoction.

Mr. Barrow, by contrast, is an unassuming real estate agent who fell into the candidate’s position after Mr. Jammeh jailed other opposition party members. He left the country last week to meet with West African leaders to discuss how to resolve the matter of taking over the presidency. Fearing for his safety at home, he wound up in Senegal, which surrounds Gambia on three sides and has a huge stake in Gambia’s stability.

Mr. Jammeh’s circle of friends has been shrinking considerably in recent days. The African Union said it would fail to recognize him as president as of Thursday. The presidents of Liberia and Mauritania met with him to urge him to step down, to no avail. His chief spokesman and longtime mouthpiece fled the country. Many of his ministers resigned.

Image

People cheered as Mr. Barrow’s motorcade passed by on Thursday in Dakar.CreditSergey Ponomarev for The New York Times

Mr. Jammeh has tried numerous channels to cling to power. He appealed to the Supreme Court, but because he had fired most of the judges and failed to replace them, a full panel could not be assembled in time to hear his case.

This week, Mr. Jammeh declared a state of emergency in the country. He shut three independent radio stations and his officers arrested people selling #GambiaHasDecided T-shirts.

For now, it appears members of the security forces are staying loyal to him. But with Mr. Jammeh lacking a broad support base, his ability to provide for supporters who have lived off his good graces for years will be challenged.

“He has nothing left, really,” said Mr. Sallah, Mr. Barrow’s spokesman. “In the end I don’t see how he can stay.”

Mariam Sakho, a Gambian waitress in Banjul, said she was confident that Mr. Jammeh would step down, especially now that many of his ministers were deserting him.

“Yesterday, my boss say I should not come to work because it is not safe,” she said. “But for me, I believe nothing will happen, so I told him I will come. Everybody is tired with Yahya Jammeh; I believe he will go.”

People stayed glued to radios for news, while many Barrow supporters celebrated openly.

An article on Friday about the arrival of foreign troops in Gambia with orders to dislodge Yahya Jammeh — the loser of last month’s presidential election, who refuses to step down — misstated the given name of a spokesman for the new president, Adama Barrow, who was inaugurated in neighboring Senegal for his own safety. The spokesman is Halifa Sallah, not Halif. The article also misquoted part of the new president’s inauguration pledge. Mr. Barrow promised to serve his country “without fear or favor,” not “without fear or failure.”