The United States is sending about 650 soldiers to the Middle East after thousands of people stormed the compound of the U.S. Embassy in Baghdad, Defense Secretary Mark Esper said Tuesday night.

Esper said President Donald Trump ordered the deployment, which came from an infantry battalion of the 82nd Airborne Division, based at Fort Bragg, North Carolina. More personnel are on standby to deploy if they're needed, he said.

"This deployment is an appropriate and precautionary action taken in response to increased threat levels against U.S. personnel and facilities, such as we witnessed in Baghdad today," Esper said in a statement. "The United States will protect our people and interests anywhere they are found around the world."

President Donald Trump, at Mar-a-Lago in Florida on New Year’s Eve, told reporters that he thinks the situation has "been handled very well" and "it’s in great shape."

"The Marines came in, we had some great warriors come in and do a fantastic job," Trump said. He also thanked the Iraqi government for its response and said, "this will not be a Benghazi," referring to the 2012 attack on a compound in Libya that killed four Americans.

The deployment was announced after a U.S. official with knowledge of the situation said on condition of anonymity that some of the thousands of protesters who stormed the embassy compound made it as far as the main reception area before startled Iraqi police joined embassy security forces to repel them.

The demonstration, which began at about 11 a.m. (3 a.m. ET), grew to as many as 6,000 people, most of them affiliated with Kataeb Hezbollah, an Iran-backed Shiite militia group, officials said. The United States blames the militia for attacks on bases of the U.S.-led coalition fighting the Islamic State in recent months.

At least 25 militia fighters were killed in U.S. airstrikes Sunday on weapons depots in Iraq and Syria that the United States said were linked to Kataeb Hezbollah. The Popular Mobilization Forces, an umbrella group for militias recognized by the Iraqi government, said live bullets and tear gas injured at least 62 demonstrators on Tuesday, a claim that NBC News couldn't independently verify.

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

Download the NBC News app for breaking news

Trump on New Year's Eve also said of Iran and military conflict: "I want to have peace; I like peace. And Iran should want peace more than anybody."

"I don’t see that happening," Trump said. "No, I don’t think Iran would want that to happen."

U.S. and Iraqi officials said the attack on the embassy compound began as a large crowd gathered after funerals for some of those who were killed in the airstrikes Sunday. The protesters marched to the embassy compound, where they tried several times to breach security and enter the main embassy building, according to a U.S. official with knowledge of the situation, who said the crowd threw Molotov cocktails over the walls and tried to burn down the entry gates.

An undetermined number of demonstrators reached the reception area, where guards screen all visitors to the embassy, before they were prevented from breaching the compound, the official said.

The embassy is secure, and U.S. personnel aren't being evacuated, the State Department said. Ambassador Matthew Tueller was away on a previously scheduled personal trip but was returning to the embassy, it said.

Members of an Iraqi Shiite militia and their supporters attack the entrance of the U.S. Embassy compound in Baghdad on Tuesday, Dec. 31, 2019. Ahmed Jalil / EPA

A defense official said about 100 Marines were rushed to the compound on V-22 Osprey tilt-rotor aircraft. Additional security was being provided by AH-64 Apache helicopters from an air weapons team, the official said.

In a potentially significant development, Iraqi police, who were taken by surprise, mobilized to work with forces of Iraq’s counterterrorism service to secure the compound, a U.S. defense official said.The Iraqi forces responded at the same time that Iraq's National Security Council was holding an emergency meeting to condemn the weekend U.S. airstrikes, suggesting that the Iraqi forces are committed to protecting the embassy. The U.S.-trained counterterrorism personnel are considered to be Iraq's most effective and most reliable security force — less vulnerable to manipulation by political leaders — according to Iraqi government and military analysts and U.S. officials.

White House press secretary Stephanie Grisham said earlier Tuesday that Trump would choose "how and when we respond to their escalation." Trump tweeted that the Iranian government "will be held fully responsible" for the safety of the embassy, saying that otherwise, Iran would "pay a very BIG PRICE."

"This is not a Warning, it is a Threat," he wrote.

....Iran will be held fully responsible for lives lost, or damage incurred, at any of our facilities. They will pay a very BIG PRICE! This is not a Warning, it is a Threat. Happy New Year! — Donald J. Trump (@realDonaldTrump) December 31, 2019

Later in the day, Iraqi Prime Minister Adil Abdul-Mahdi and President Barham Salih reassured Secretary of State Mike Pompeo in separate telephone conversations that they took the responsibility seriously, the State Department said. Abdul-Mahdi also issued a statement warning that "any aggressive behavior against foreign embassies" would be "strictly stopped by security forces" and punished.

The clashes Tuesday follow anti-government protests in recent months in which hundreds of people have been killed, according to protest groups, most of them by Iraqi security forces. The mass uprisings prompted the resignation last month of Abdul-Mahdi, who remains prime minister in a caretaker capacity.

The United States blames Kataeb Hezbollah for 11 attacks on bases used by the U.S.-led coalition over the past two months. The latest came on Friday, when a rocket attack killed a U.S. contractor and injured four U.S. service members.

The U.S. airstrikes have been met with an angry reaction from both Iran and Iraq.

The United States has about 5,000 personnel in Iraq to train and assist government troops in the fight against ISIS. But the Iraqi government is also allied with a powerful network of militia groups, many of which are backed by or linked to Tehran.

Tehran called the U.S. airstrikes "terrorism," and Iraq called them a "violation" of its sovereignty. The militia has vowed to respond.