By Joanne Chiu and Chong Koh Ping

Investors have overcome initial discomfort at China's tightening grip on Hong Kong, focusing instead on how a tough new security law could curb unrest and encourage an influx of mainland money.

On Thursday, the city's benchmark Hang Seng stock index climbed 2.9%, with property and financial stocks leading broad-based gains. Major advancers included some of the mainland-China-focused stocks that make up much of the index, as well as local companies such as Hong Kong Exchanges & Clearing Ltd. and Hang Lung Properties Ltd. HSBC Holdings PLC rose 3%.

With markets closed Wednesday for a public holiday, this was the first trading session since Beijing imposed a national-security law on Hong Kong, giving China's central government a much stronger hand in policing dissent in the city.

When China's plans for such legislation were first unveiled in late May, the Hang Seng Index suffered its largest one-day percentage decline since July 2015, tumbling 5.6%. But the index is now 3.5% above where it closed May 21, the day before the selloff.

Christopher Cheung, a pro-Beijing lawmaker in Hong Kong who represents brokerages in the city, said markets viewed the new law positively as it made major protests less likely, pointing to comparatively small demonstrations on Wednesday.

"Investors prefer stability over uncertainty. The July 1 protest is proof of that," he said. Thousands of demonstrators took to the streets Wednesday, in defiance of a ban, but the numbers were a far cry from the biggest crowds of last summer.

Mr. Cheung said many investors also expect Beijing to offer sweeteners to Hong Kong as the central government seeks to integrate the city more closely with neighboring areas of southern China.

Francis Lun, chief executive at Geo Securities Ltd., said, "There's a disconnect between the financial market and the social discontent among the general public." He added that "Hong Kong's financial market has been, and will always be, dependent on China's support."

Mr. Lun and others said one factor buoying the local market recently has been secondary listings from U.S.-traded Chinese companies such as NetEase Inc. and JD.com Inc., as tensions rise between the two countries. These share sales have drawn a lot of investor interest and have helped buoy shares of Hong Kong Exchanges & Clearing, the exchange operator, which is also a major index constituent.

David Chao, global market strategist for Asia Pacific ex-Japan at Invesco, said such secondary listings had helped boost the market, as had bargain-hunting by mainland investors, focusing in particular on Chinese stocks that trade at a discount to peers listed in Shenzhen and Shanghai.

"The Hang Seng Index is the cheapest index that I see right now across Asia," said Mr. Chao. That benchmark trades on a price of about 11.3 times expected earnings, according to Refinitiv Datastream.

The rally also signals more investor confidence that Hong Kong will be able to attract mainland tourists and further investment from China, he added.

Still, the 50-member Hang Seng Index remains down 10.9% for the year, lagging behind some peers. In mainland China, the Shanghai Composite Index rose 2.1% on Thursday, joining its peer the CSI 300 in positive territory for the year. The S&P 500 is down about 3.6% year to date.

Hong Kong also faces challenging economic conditions, after last year enduring its first full-year recession in a decade.

Adrienne Lui, an economist at Citigroup Global Markets, said in a note to clients that the new law will likely reduce some risks of large-scale disruptions to businesses, but that the international business community would closely watch trends in how the law was implemented.

"Overall, Hong Kong likely faces a long, winding road to economic recovery," she said. The bank expects the Hong Kong economy to shrink 4.6% in 2020.

Write to Joanne Chiu at joanne.chiu@wsj.com and Chong Koh Ping at chong.kohping@wsj.com