MEXICO CITY — Five people were killed early Monday at a crowded nightclub in the popular Caribbean tourist destination of Playa del Carmen, Mexico, when an armed man who had been denied entry opened fire inside the club, prompting a gun battle and a stampede, officials said.

Four of the victims, including three members of the security team that had blocked the gunman’s entry, were hit by bullets, and the fifth victim was trampled in the resulting pandemonium, the authorities said.

The shooting occurred at the Blue Parrot nightclub, which was hosting the final night of the BPM Festival, an international electronic music event that draws fans from around the world.

The gunman entered the club around 2:30 a.m., but was turned away at the door because he was carrying a weapon, said Miguel Ángel Pech, the state attorney for Quintana Roo State, on the Yucatán Peninsula. The man then opened fire, and as patrons threw themselves to the ground or rushed for the exits, security guards at the club “repelled” the attacker, Mr. Pech said, adding that the guards were apparently armed.