The attack was claimed by the Baluchistan Liberation Army, a separatist group that has attacked security forces and Chinese installations in the past, including the Chinese Consulate in Karachi last year.

Pakistani officials said that the attackers had intended to target and take hostages but were denied entry into the main hall by hotel security guards, which forced the gunmen to take the stairs leading to upper floors.

Members of the army, navy and the police were able to get the guests in a safe area soon after the attack, which started around 4:45 p.m. Saturday, and held the attackers to a fourth-floor corridor. The attackers destroyed hotel CCTV cameras and planted explosive devices at entry points leading to the fourth floor, officials said. Security forces “made special entry points to get into the fourth floor, shot down all terrorists and cleared the planted I.E.D.s,” the military statement said.

The separatist group claimed that four people had carried out the attack, but Pakistani officials disputed that figure, saying only three attackers had been involved. The military released pictures that it said showed their bodies in the wreckage of the hotel corridors.

Baluchistan is the largest of Pakistan’s four provinces, and although sparsely populated it is rich in mineral and natural resources, including copper, gold and natural gas. Ethnic Baluch have long demanded greater financial autonomy and freedom from the federal government, and some separatist groups, including the Baluchistan Liberation Army, have vowed to create a separate homeland for the Baluch.

China, a close ally of Pakistan, is investing heavily in Baluchistan, and a deepwater port in Gwadar is one of its primary investments in the country. China is estimated to have spent around $62 billion on the so-called China-Pakistan Economic Corridor, a project that is part of China’s Belt and Road Initiative, a $1 trillion chain of infrastructure development programs stretching across about 70 countries.

Since the initiative began in 2013, Pakistan has served as its flagship site. China has used the partnership with Pakistan, a strategically located and nuclear-armed neighbor, to advance its own military and economic interests. Its plan for Gwadar includes a large special economic zone for Chinese companies.