For three days, Iranian military officials knew they had shot down a Ukrainian jetliner while the government issued false statements, denying any responsibility.

Credit...Arash Khamooshi for The New York Times

When the Revolutionary Guards officer spotted what he thought was an unidentified aircraft near Tehran’s international airport, he had seconds to decide whether to pull the trigger.

Iran had just fired a barrage of ballistic missiles at American forces, the country was on high alert for an American counterattack, and the Iranian military was warning of incoming cruise missiles.

The officer tried to reach the command center for authorization to shoot but couldn’t get through. So he fired an antiaircraft missile. Then another.

The plane, which turned out to be a Ukrainian jetliner with 176 people on board, crashed and exploded in a ball of fire.

Within minutes, the top commanders of Iran’s Revolutionary Guards realized what they had done. And at that moment, they began to cover it up.

For days, they refused to tell even President Hassan Rouhani, whose government was publicly denying that the plane had been shot down. When they finally told him, he gave them an ultimatum: come clean or he would resign.

Only then, 72 hours after the plane crashed, did Iran’s supreme leader, Ayatollah Ali Khamenei, step in and order the government to acknowledge its fatal mistake.

The New York Times pieced together a chronology of those three days by interviewing Iranian diplomats, current and former government officials, ranking members of the Revolutionary Guards and people close to the supreme leader’s inner circle and by examining official public statements and state media reports.

The reporting exposes the government’s behind-the-scenes debate over covering up Iran’s responsibility for the crash while shocked Iranians, grieving relatives and countries with citizens aboard the plane waited for the truth.

The new details also demonstrate the outsize power of the Islamic Revolutionary Guards Corps, which effectively sidelined the elected government in a moment of national crisis, and could deepen what many Iranians already see as a crisis of legitimacy for the Guards and the government.

The bitter divisions in Iran’s government persist and are bound to affect the investigation into the crash, negotiations over compensation and the unresolved debate over accountability.

Around midnight on Jan. 7, as Iran was preparing to launch a ballistic-missile attack on American military posts in Iraq, senior members of the Islamic Revolutionary Guards Corps deployed mobile antiaircraft defense units around a sensitive military area near Tehran’s Imam Khomeini Airport.

Iran was about to retaliate for the American drone strike that had killed Iran’s top military commander, Gen. Qassim Suleimani, in Baghdad five days earlier, and the military was bracing for an American counterstrike. The armed forces were on “at war” status, the highest alert level.

But in a tragic miscalculation, the government continued to allow civilian commercial flights to land and take off from the Tehran airport.

Gen. Amir Ali Hajizadeh, commander of the Guards’ Aerospace Force, said later that his units had asked officials in Tehran to close Iran’s airspace and ground all flights, to no avail.

Iranian officials feared that shutting down the airport would create mass panic that war with the United States was imminent, members of the Guards and other officials told The Times. They also hoped that the presence of passenger jets could act as a deterrent against an American attack on the airport or the nearby military base, effectively turning planeloads of unsuspecting travelers into human shields.

After Iran’s missile attack began, the central air defense command issued an alert that American warplanes had taken off from the United Arab Emirates and that cruise missiles were headed toward Iran.

The officer on the missile launcher near the airport heard the warnings but did not hear a later message that the cruise missile alert was a false alarm.

The warning about American warplanes may have also been wrong. United States military officials have said that no American planes were in or near Iranian airspace that night.

When the officer spotted the Ukrainian jet, he sought permission to fire. But he was unable to communicate with his commanders because the network had been disrupted or jammed, General Hajizadeh said later.

The officer, who has not been publicly identified, fired two missiles, less than 30 seconds apart.

General Hajizadeh, who was in western Iran supervising the attack on the Americans, received a phone call with the news.

“I called the officials and told them this has happened and it’s highly possible we hit our own plane,” he said later in a televised statement.

By the time General Hajizadeh arrived in Tehran, he had informed Iran’s top three military commanders: Maj. Gen. Abdolrahim Mousavi, the army’s commander in chief, who is also the chief of the central air defense command; Maj. Gen. Mohammad Bagheri, chief of staff of the Armed Forces; and Maj. Gen. Hossein Salami, commander in chief of the Revolutionary Guards.

The Revolutionary Guards, an elite force charged with defending Iran’s clerical rule at home and abroad, is separate from the regular army and answers only to the supreme leader. At this point, the leaders of both militaries knew the truth.

General Hajizadeh advised the generals not to tell the rank-and-file air defense units for fear that it could hamper their ability to react quickly if the United States did attack.

“It was for the benefit of our national security because then our air defense system would be compromised,” Mr. Hajizadeh said in an interview with Iranian news media this week. “The ranks would be suspicious of everything.”

The military leaders created a secret investigative committee drawn from the Guards’ aerospace forces, from the army’s air defense, and from intelligence and cyberexperts. The committee and the officers involved in the shooting were sequestered and ordered not to speak to anyone.

The committee examined data from the airport, the flight path, radar networks, and alerts and messages from the missile operator and central command. Witnesses — the officer who had pulled the trigger, his supervisors and everyone involved — were interrogated for hours.

The group also investigated the possibility that the United States or Israel may have hacked Iran’s defense system or jammed the airwaves.

By Wednesday night, the committee had concluded that the plane was shot down because of human error.

“We were not confident about what happened until Wednesday around sunset,” General Salami, the commander in chief of the Guards, said later in a televised address to the Parliament. “Our investigative team concluded then that the plane crashed because of human errors.”

Ayatollah Khamenei was informed. But they still did not inform the president, other elected officials or the public.

Senior commanders discussed keeping the shooting secret until the plane’s black boxes — the flight data and cockpit voice recorders — were examined and formal aviation investigations completed, according to members of the Guards, diplomats and officials with knowledge of the deliberations. That process could take months, they argued, and it would buy time to manage the domestic and international fallout that would ensue when the truth came out.

The government had violently crushed an anti-government uprising in November. But the American killing of General Suleimani, followed by the strikes against the United States, had turned public opinion around. Iranians were galvanized in a moment of national unity.

The authorities feared that admitting to shooting down the passenger plane would undercut that momentum and prompt a new wave of anti-government protests.

“They advocated covering it up because they thought the country couldn’t handle more crisis,” said a ranking member of the Guards who, like others interviewed for this article, spoke on condition of anonymity to discuss internal deliberations. “At the end, safeguarding the Islamic Republic is our ultimate goal, at any cost.”

That evening, the spokesman for the Joint Armed Forces, Brig. Gen. Abolfazl Shekarchi, told Iranian news media that suggestions that missiles struck the plane were “an absolute lie.”

On Thursday, as Ukrainian investigators began to arrive in Tehran, Western officials were saying publicly that they had evidence that Iran had accidentally shot down the plane.

A chorus of senior Iranian officials — from the director of civil aviation to the chief government spokesman — issued statement after statement rejecting the allegations, their claims amplified on state media.

The suggestion that Iran would shoot down a passenger plane was a “Western plot,” they said, “psychological warfare” aimed at weakening Iran just as it had exercised its military muscle against the United States.

But in private, government officials were alarmed and questioning whether there was any truth to the Western claims. Mr. Rouhani, a seasoned military strategist himself, and his foreign minister, Javad Zarif, deflected phone calls from world leaders and foreign ministers seeking answers. Ignorant of what their own military had done, they had none to give.

Domestically, public pressure was building for the government to address the allegations.

Among the plane’s passengers were some of Iran’s best and brightest. They included prominent scientists and physicians, dozens of Iran’s top young scholars and graduates of elite universities, and six gold and silver medal winners of international physics and math Olympiads.

There were two newlywed couples who had traveled from Canada to Tehran for their weddings just days earlier. There were families and young children.

Their relatives demanded answers. Iranian social media began to explode with emotional commentary, some accusing Iran of murdering its own citizens and others calling such allegations treason.

Persian-language satellite channels operating from abroad, the main source of news for most Iranians, broadcast blanket coverage of the crash, including reports from Western governments that Iran had shot down the plane.

Mr. Rouhani tried several times to call military commanders, officials said, but they did not return his calls. Members of his government called their contacts in the military and were told the allegations were false. Iran’s civil aviation agency called military officials with similar results.

“Thursday was frantic,” Ali Rabiei, the government spokesman, said later in a news conference. “The government made back-to-back phone calls and contacted the armed forces asking what happened, and the answer to all the questions was that no missile had been fired.”

On Friday morning, Mr. Rabiei issued a statement saying the allegation that Iran had shot down the plane was “a big lie.”

Several hours later, the nation’s top military commanders called a private meeting and told Mr. Rouhani the truth.

Mr. Rouhani was livid, according to officials close to him. He demanded that Iran immediately announce that it had made a tragic mistake and accept the consequences.

The military officials pushed back, arguing that the fallout could destabilize the country.

Mr. Rouhani threatened to resign.

Canada, which had the most foreign citizens on board the plane, and the United States, which as Boeing’s home country was invited to investigate the crash, would eventually reveal their evidence, Mr. Rouhani said. The damage to Iran’s reputation and the public trust in the government would create an enormous crisis at a time when Iran could not bear more pressure.

As the standoff escalated, a member of Ayatollah Khamenei’s inner circle who was in the meeting informed the supreme leader. The ayatollah sent a message back to the group, ordering the government to prepare a public statement acknowledging what had happened.

Mr. Rouhani briefed a few senior members of his government. They were rattled.

Mr. Rabiei, the government spokesman who had issued a denial just that morning, broke down. Abbas Abdi, a prominent critic of Iran’s clerical establishment, said that when he spoke to Mr. Rabiei that evening, Mr. Rabiei was distraught and crying.

“Everything is a lie,” Mr. Rabiei said, according to Mr. Abdi. “The whole thing is a lie. What should I do? My honor is gone.”

Mr. Abdi said the government’s actions had gone “far beyond” just a lie.

“There was a systematic cover-up at the highest levels that makes it impossible to get out of this crisis,” he said.

Iran’s National Security Council held an emergency meeting and drafted two statements, the first to be issued by the Joint Armed Forces followed by a second one from Mr. Rouhani.

As they debated the wording, some suggested claiming that the United States or Israel may have contributed to the accident by jamming Iran’s radars or hacking its communications networks.

But the military commanders opposed it. General Hajizadeh said the shame of human error paled compared with admitting his air defense system was vulnerable to hacking by the enemy.

Iran’s Civil Aviation Agency later said that it had found no evidence of jamming or hacking.

At 7 a.m., the military released a statement admitting that Iran had shot down the plane because of “human error.”

The bombshell revelation has not ended the division within the government. The Revolutionary Guards want to pin the blame on those involved in firing the missiles and be done with it, officials said. The missile operator and up to 10 others have been arrested but officials have not identified them or said whether they had been charged.

Mr. Rouhani has demanded a broader accounting, including an investigation of the entire chain of command. The Guards’ accepting responsibility, he said, is “the first step and needs to be completed with other steps.” His spokesman and lawmakers have demanded to know why Mr. Rouhani was not immediately informed.

Mr. Rouhani touched on that concern when he put out his statement an hour and 15 minutes later. The first line said that he had found out about the investigative committee’s conclusion about cause of the crash “a few hours ago.”

It was a stunning admission, an acknowledgment that even the nation’s highest elected official had been shut out from the truth, and that as Iranians, and the world, turned to the government for answers, it had peddled lies.

“What we thought was news was a lie. What we thought was a lie was news,” said Hesamedin Ashna, Mr. Rouhani’s top adviser, on Twitter. “Why? Why? Beware of cover-ups and military rule.”