As returns coming in from major regions clearly indicated that he was going to lose, Mr. Jammeh asked his key advisers to annul the votes, the official said. He then gathered at the statehouse his top military security advisers, police officers and intelligence officials and asked for their support to discredit the vote.

The officers told him that chaos would break out if they did so. Tempers flared at the meeting, said the official, who declined to be named because of the top-secret nature of the gathering. But eventually, Mr. Jammeh agreed to concede.

Speculation had been rampant that Mr. Jammeh had fled the country in the wake of the election, but the official said Mr. Jammeh remained in Gambia.

On Saturday, the streets in the capital, Banjul, were largely calm. But Friday night, thousands celebrated across Gambia, the smallest nation on continental Africa. Young people burned posters with photographs of Mr. Jammeh and currency bills, which bear his image. Even the inspector general of the police was spotted among the crowd of celebrants.

This year in the period before the election, the security forces arrested more than 90 opposition activists for participating in peaceful protests. Thirty activists, including the leader of the largest opposition party, the United Democratic Party, were prosecuted and sentenced to three years in prison. Two other opposition protesters died in custody, including the opposition party’s national organizing secretary, Solo Sandeng, who was beaten to death at the country’s National Intelligence Agency in April, according to an Amnesty International report.

Human rights groups tried to draw attention to Mr. Jammeh’s abuses with numerous reports outlining deaths and torture suffered by his opponents. Western nations criticized Mr. Jammeh and threatened sanctions. He began courting nations in the Middle East for aid.

On Saturday, Secretary of State John Kerry issued a statement hailing “the first democratic transfer of power in the country.”