Last May, members of the Communist Party of Nepal (Maoist) - which holds the most seats in parliament - took to the streets, demanding the prime minister's reisgnation so the party could form a new government.
The Maoists - former rebels who laid down their arms to join a peace process and then won the most seats in 2008 elections - had previously run the government.
But the Maoist government resigned over differences with the president over the implementation of the peace process that ended their decade-long insurgency. They were frustrated at the lack of progress on integrating their fighters into the army.
Prime Minister Madhav Kumar Nepal then took over, but the Maoists immediately called a general strike, and he eventually resigned under the pressure in late June.
The government has been deadlocked since. No candidate was able to secure a majority in the 16 votes held over the past several months, and the parties were unable to agree on a coalition government.
But on Thursday in the 17th vote, Jhalnath Khanal, leader of the Communist Party of Nepal (United Marxist-Leninist), secured 368 of 601 votes - enough to become prime minister.
Khanal's party is only the third largest party in the parliament but got support from the Maoists.
The political stalemate has set back progress on a constitution and a peace process that ended a Maoist insurgency, which claimed thousands of lives.
Maoist leader Pushpa Kamal Dahal, who himself was a candidate, said the party had decided to support Khanal to end the stalemate.
Khanal, 60, is a veteran politician who was involved in the pro-democracy protests against the monarchy in 1990 and again in 2006. This would be his first time serving as the prime minister.
Now the government must turn to the fate of the thousands of former Maoist fighters who are still in camps and the writing of a new constitution.