KATHMANDU, Nepal — Communist parties in Nepal with closer ties to neighboring China have emerged victorious in the country’s largest democratic exercise ever.

A powerful political coalition of two communist parties led by former prime ministers, Pushpa Kamal Dahal and K. P. Sharma Oli, won a majority of the contested seats in two legislative bodies, the Parliament and the Provincial Assembly.

Vote counting began shortly after polls closed on Dec. 7, but the results did not become clear until this week. On Friday, a spokesman for Nepal’s election commission said his office had to tally up only a small number of votes.

The defeat of Prime Minister Sher Bahadur Deuba’s governing Nepali Congress party is likely to have significant foreign policy implications for Nepal, a landlocked country squeezed between India and China.