HONG KONG—China’s Communist Party was subjected to an embarrassing lesson in democracy, as its offer of direct elections for the chief executive in Hong Kong was voted down.

After a year of turmoil and deepening divisions in the city over a proposal critics derided, it wasn’t a surprise that the election blueprint was rejected, but the optics of its demise added to the insult for Beijing.

After most of the plan’s supporters walked out of the legislative chamber in a failed attempt to prevent a quorum, the vote, which required a two-thirds majority in the 70-member legislature to pass, took place in a cloud of confusion. The result: a resounding rejection with 28 votes against and only eight in favor.

Since the 1997 handover from Britain, Hong Kong’s chief executive has been picked by a heavily pro-Beijing nominating committee. Beijing’s plan would have given the city’s voters the right to directly elect its top official, but only from a list of preapproved candidates.

The fiasco in the legislature capped a year in which images of student protesters fending off police pepper spray with umbrellas and holding their ground against tear gas grabbed attention and sympathy around the world. Months of protests that shut down the city’s thoroughfares highlighted opposition to a plan Beijing tried to sell as the most democratic the city had ever seen.