Authorities were investigating it as an act of terrorism.
The exact death toll appeared to be in flux. The Orlando mayor referred at a news conference to as many as 50 casualties and 53 more people who were hospitalized. But it was unclear whether the casualties referred to the number of dead.
"There's blood everywhere," Mayor Buddy Dyer said.
The suspect exchanged gunfire with an officer working at the gay club known as Pulse Orlando around 2 a.m., when more than 300 people were inside. The gunman then went back inside and took hostages, Police Chief John Mina said.
Around 5 a.m., authorities sent in a SWAT team to rescue the hostages. Police have not determined an exact number of casualties, but Mina said "approximately 20" bodies were inside the club.
In addition to the guns, the shooter also had some sort of "suspicious device," Mina said.
Authorities were looking into whether the attack was an act of domestic or international terror, and if the shooter acted alone, according to Danny Banks, an agent with the Florida Department of Law Enforcement.
"This is an incident, as I see it, that we certainly classify as domestic terror incident," Orange County Sheriff Jerry Demings said.
The suspect was identified as Omar Mateen of Port St. Lucie, Florida.
Rep. Alan Grayson named the shooter, citing law enforcement officials. A federal law enforcement official with knowledge of the investigation also confirmed the name. The official was not authorized to discuss the investigation publicly and spoke on condition of anonymity.
FBI agent Ron Hopper said there was no further threat to Orlando or the surrounding area.
When asked if the gunman had a connection to radical Islamic terrorism, Hopper said authorities had "suggestions that individual has leanings towards that."
Police had said previously on Twitter that there was a "controlled explosion" at the scene of the shooting. Mina said that noise was caused by a device intended to distract the shooter.
A woman who was outside the dance club early Sunday was trying to contact her 30-year-old son, Eddie, who texted her when the shooting happened and asked her to call police. He told her he ran into a bathroom with other club patrons to hide. He then texted her: "He's coming."
"The next text said: 'He has us, and he's in here with us,'" Mina Justice said. "That was the last conversation."
Dozens of police vehicles swarmed the area around the club. At least two police pickup trucks were seen taking what appeared to be shooting victims to the Orlando Regional Medical Center.
Pulse posted on its own Facebook page around 2 a.m.: "Everyone get out of pulse and keep running." Just before 6 a.m., the club posted an update: "As soon as we have any information, we will update everyone. Please keep everyone in your prayers as we work through this tragic event. Thank you for your thoughts and love."
Local, state and federal agencies were investigating.
President Barack Obama was briefed on the attack and has asked for regular updates on the investigation, the White House said.
The attack follows the fatal shooting late Friday of 22 -year-old singer Christina Grimmie, who was killed after her concert in Orlando by a 27-year-old Florida man who later killed himself. Grimmie was a YouTube sensation and former contestant on "The Voice."
Jon Alamo said he was at the back of one of the club's rooms when a man holding a weapon came into the front of the room.
"I heard 20, 40, 50 shots," Alamo said. "The music stopped."
Club-goer Rob Rick said the shooting started just before closing time.
"Everybody was drinking their last sip," he said.
He estimated more than 100 people were still inside when he heard shots, got on the ground and crawled toward a DJ booth. A bouncer knocked down a partition between the club area and an area in the back where only workers are allowed. People inside were able to then escape through the back of the club.
Christopher Hansen said he was in the VIP lounge when he heard gunshots. He continued to hear shooting even after he emerged and police urged people to back away from the club. He saw the wounded being tended to across the street.
"I was thinking, 'Are you kidding me?' So I just dropped down. I just said, 'Please, please, please, I want to make it out,'" he said. "And when I did, I saw people shot. I saw blood. You hope and pray you don't get shot."
Associated Press Writer Eric Tucker in Washington contributed to this report.
This story has been corrected to show that Banks is with the Florida Department of Law Enforcement, not the FBI.