ORLANDO, Florida — The gunman who opened fire inside a crowded nightclub here early Sunday morning, launching a rampage that killed 50 people and injured 53 others in the deadliest shooting spree in the country’s history, had pledged allegiance to the Islamic State before the attack, according to U.S. law enforcement officials.

President Barack Obama said the FBI was investigating the shooting as an act of terrorism, an announcement he made about 12 hours after the gunfire began at Pulse, a popular gay bar and dance club.

“We know enough to say that this was an act of terror and an act of hate,” Obama said during remarks at the White House. “And as Americans, we are united in grief, in outrage, and in resolve to defend our people.”

Police said that after a first round of gunshots at 2 a.m., the shooter took hostages for about three hours, until officers who went inside to rescue these people killed the gunman in a shootout. It was not immediately clear if the death toll included the gunman.

The gunman, identified as 29-year-old Omar Mateen, apparently made a 911 call before the attack identifying himself and pledging allegiance to the leader of the Islamic State, according to U.S. law enforcement officials.

Mateen also made a reference to the 2013 bombing of the Boston Marathon during this call, officials said. Much like Tamerlan Tsarnaev, the older of the two brothers who carried out that attack, Mateen had also been the focus of an FBI investigation before carrying out an attack.

Ron Hopper of the FBI said that Mateen had twice been investigated by the bureau and that in both cases, he was investigated before the probes were concluded.

In 2013, Hopper said agents investigated Mateen after he made “inflammatory comments to co-workers alleging possible ties to terrorists.” Mateen was interviewed twice and, when investigators were unable to verify the details of his comments, the FBI closed the probe, Hopper said.

Mateen was investigated again in 2014 by the FBI. This time, they looked into potential ties connecting Mateen to Moner Mohammad Abusalha, the first American to carry out a suicide attack in Syria. Like Maten, Abusalha lived in Fort Pierce, Florida.

“We determined that contact was minimal and did not constitute a substantive relationship or a threat at that time,” Hopper said.

Mateen legally bought the two guns believed to be used in the attack legally within “the last few days,” according to the Bureau of Alcohol, Tobacco, Firearms and Explosives.

In a message Sunday, the Islamic State appeared to take credit for the attack, but it remains unclear if the gunman had any connections to the group or if they had any prior knowledge.

Police had said earlier Sunday that 20 people were killed before saying that the toll was significantly higher. Until Sunday, the 2007 rampage at Virginia Tech — which saw 32 people killed and 30 others injured — was the country’s worst mass shooting.

Orlando Police Chief John Mina said that the toll from this latest mass slaughter could have been even greater, saying that a SWAT team “rescued at least 30 possible victims and brought them to safety.” Police said they were not able to say if all of the people killed or injured were shot during the initial burst of gunfire at 2 a.m. or during the shootout with 11 police officers three hours later.

“It’s absolutely terrible,” Mina said during a news briefing. “Fifty victims in one location, one shooting, is absolutely one of the worst tragedies we’ve seen.”

Rep. Adam Schiff, D-California, ranking member of the House Permanent Select Committee on Intelligence, said he was briefed Sunday and told that local law enforcement officials said the gunman had “declared his allegiance to ISIS.”

A U.S. official said that Department of Homeland Security reports being circulated to government authorities are “referring to local law enforcement reports saying the shooter pledged loyalty to ISIS and was heard praying in another language in the nightclub.”

Just after a husband and wife killed 14 people last December in a terror attack in San Bernardino, California, one of the shooters went on Facebook and pledged her allegiance to the emir of the self-proclaimed Islamic State. Officials later said the posting was made on behalf of both attackers.

The group said in a message posted Sunday by the Islamic State-linked Amaq News Agency that the Orlando shooting “was carried out by an Islamic State fighter.” The same news agency had released a message showing the Islamic State claiming some credit for the San Bernardino attack two days after that occurred, while Amaq posted a statement from the group asserting responsibility for the Brussels attacks in March within hours.

The Islamic State has repeatedly executed gay people and then released videos showing these gruesome executions.

Hopper said officials had not found any indications of outside help or another suspect, and added that they were confident there were no additional threats.

Also on Sunday , a man was detained in Santa Monica, California, by police while possessing weapons and dangerous material, police said. The Los Angeles County Sheriff’s Department said that they were investigating any possible link between this man and a Gay Pride Parade and Festival in West Hollywood on Sunday, though they added that there have been “no specific or credible threats” in the Los Angeles area.

Mateen’s ex-wife said that he was abusive, saying in an interview Sunday that he beat her repeatedly during their marriage. She said that during their brief marriage, Mateen, who was Muslim, was not very religious and gave no indications that he was devoted to radical Islam.

Mina said it appeared the gunman was armed with “a handgun and an AR-15-type assault rifle” and had additional rounds on him.

“It appears he was organized and well-prepared,” Mina said during another briefing.

An AR-15 is the civilian variant of the military M-16 rifle. It is one of the most popular weapons in the United States and can be customized with a variety of accessories including different grips and sights. A standard magazine for it carries about 30 bullets.

A number of federal agencies were participating in a massive law enforcement response to the shooting in Orlando, authorities said. On Sunday afternoon, Attorney General Loretta E. Lynch, who had been scheduled to attend a meeting in Beijing on cybersecurity, said she was returning to Washington so she could monitor the ongoing investigation.

“We stand with the people of Orlando, who have endured a terrible attack on their city,” Obama said during a statement he made at the White House after meeting with FBI Director James Comey.

Obama said it was too early to know “the precise motivations of the killer,” but he said the FBI would investigate any possible links the gunman had to terrorist groups.

During his remarks Sunday afternoon, Obama — echoing comments he has made during numerous other statements given after shooting rampages — said the bloodshed in Orlando served as a reminder of how easily people can obtain guns in the United States. Pope Francis also expressed deep “horror and condemnation” of the shooting on Sunday, according to the Associated Press.

Obama also signed a proclamation honoring the victims and ordering that American flags be flown at half-staff until sunset on Thursday.

“We’re dealing with something we never imagined and is unimaginable,” Orlando Mayor Buddy Dyer said during a news briefing Sunday.

Dyer said he had issued a state of emergency in the city, while Republican Gov. Rick Scott said he was issuing a similar order for surrounding Orange County and called the shooting “clearly an act of terror.”

Details about Mateen’s background were scarce on Sunday morning. His family is from Afghanistan, while Mateen is believed to have been born in the United States. His father hosted a political television show in Afghanistan and, in one video, declared his candidacy for the Afghan presidency.

“I unequivocally condemn the horrific attack in Orlando, Florida, USA. Nothing can justify killing of civilians,” Afghan President Ashraf Ghani wrote on Twitter. “My sympathies and thoughts are with the families of the victims, the American people and the US government.”

One relative said that Mateen’s family was in shock after being told on Sunday morning about his involvement. This relative said Mateen’s family was very sorry about what had happened.

“We had a crime that will have a lasting effect on our community,” Dyer said. “We need to stand strong, we need to be supportive of the victims and their families.”

Police said that during the shootout with the gunman, one Orlando police officer was shot and saved by his Kevlar helmet.

Numerous people injured were taken to Orlando Regional Medical Center, which was locked down Sunday morning along with two other hospitals. OneBlood, a foundation with blood collection centers throughout the southeast, put out a call for donors after the shooting.

While many people sharing the call on social media reported that OneBlood is allowing all gay men to donate — going against Food and Drug Administration guidelines — a spokesman for the foundation said that’s not true and that it was adhering to longstanding federal restriction on gay men from donating blood. (While the Food and Drug Administration recently updated its guidelines to allow men who have not had sex with another man within a year’s time to donate blood, the spokesman said OneBlood’s system hasn’t been updated to allow that yet.)

The horrific attack began as Saturday gave way to Sunday at the crowded club. Around 2 a.m., Pulse Orlando posted an urgent message on Facebook: “Everyone get out of pulse and keep running.”

Within minutes of the shooting, police vehicles and a SWAT team descended on the club, which had more than 300 people inside as the shooting began.

“I was there,” Ricardo J. Negron posted on the club’s Facebook page several hours later. “Shooter opened fire @ around 2:00am. People on the dance floor and bar got down on the floor and some of us who were near the bar and back exit managed to go out through the outdoor area and just ran. I am safely home and hoping everyone gets home safely as well.”

RELATED: Survivors of Orlando massacre recall struggle to stay alive

An officer working at the club exchanged fire with the gunman, authorities said. It was then, according to police, that the incident developed into “a hostage situation.”

Authorities said the man was armed with a “suspicious device,” in addition to his guns, Mina, the police chief, told reporters.

About three hours after the initial reports of gunfire, the SWAT team launched a rescue operation and killed the gunman, authorities said.

“The decision was made to rescue hostages that were in there,” Mina said.

Police later reported that a sound heard at the club was a “controlled explosion.”

Joshua and Mary Zika live less than a block from the nightclub, and say they’ve never had any problems with the club before. “It might sound weird to have a nightclub in the middle of a residential neighborhood,” Mary Zika said.

They say they didn’t hear any shots or notice anything out of the ordinary, because helicopters are often heard in the area due to the nearby hospital. Instead, they found out about it from the news, reading and watching in disbelief as they realized this had happened just outside their door.

Both said they were particularly appalled that the shooter had appeared to target this particular population.

“We’re proud of our gay community in Orlando,” Mary Zika said.

Rob Rick said the violence erupted as the night was winding down.

“Everybody was drinking their last sip,” he said.

U.S. Sen. Angus King, I-Maine, a member of the Senate Select Committee on Intelligence, in a statement urged the country to “stand united in the face of hatred and violence. Let us join together to show that, although our hearts may be broken, our love for one another remains strong.”

U.S. Sen. Susan Collins, also a member of the Senate Select Committee on Intelligence, said, “This is the worst attack on our country since 9-11-01 and the worst mass shooting in our history. I am heartbroken for the loved ones of the victims of this horrifying act of terrorism. If the FBI’s investigation confirms allegations that the attacker pledged his allegiance to ISIS or if he proves to have been a lone wolf, this attack is yet another stark reminder that the United States is not immune from the threat of violent extremism.”

U.S. Rep. Bruce Poliquin, R-Maine, called the shooting in Orlando “a national tragedy.”

“My heart and prayers are with everyone involved in this horrific terror attack in Orlando,” Poliquin said.

U.S. Rep. Chellie Pingree, D-Maine, said, “Once again our country has been struck with an act of senseless violence and murder. We should put aside ideological and political difference and together stand up against acts of gun violence, hate crimes and domestic terrorism. My thoughts are with the families who have lost someone, the survivors and the police officers and first responders who put their own lives at risk today in Florida. “

More than 150 people gathered in Portland on Sunday evening for a vigil in remembrance of those killed in Orlando. The Bridge Alliance announced on social media Sunday that it will hold a candlelight vigil starting around 8:30 p.m. Monday on the steps of Bangor City Hall.

The early-Sunday rampage followed the fatal shooting Friday night of a pop singer who was killed while signing autographs following a performance at an Orlando concert venue. Christina Grimmie, a 22-year-old singer who was a finalist on NBC’s show “The Voice,” died hours after being shot by a gunman who then shot himself, police said.