60-year-old Khanal, leader of the Communist Party of Nepal (United Marxist-Leninist), secured 368 of the 557 votes cast in the 601-member Parliament, the speaker announced.
Khanal's party, which is the third largest party in the parliament and leads the caretaker coalition, got support from the main Opposition Maoist Party.
Earlier, in a surprise move, CPN-Maoist withdrew Prachanda from the Prime Ministerial race and decided to support Khanal.
Nepal's parliament voted for a new prime minister under revised election rules designed to break a deadlock that had left the country without a government since the June 30 resignation of Prime Minister Madhav Kumar Nepal under intense Maoists pressure.
Under the new rules, no lawmaker was to be allowed to abstain from voting in the House.
The UCPN-Maoist has 238 seats and the CPN-UML has 108 seats in the Constituent Assembly, which acts as the country's interim Parliament.
The second largest Nepali Congress, which put up R.C Poudyal as its candidate again, received 122 votes. The party has 114 members in the House.
Bijaya Gachhadar, the president of Madhesi Peoples Rights Forum -Democratic, who was the third candidate got 67 votes.
President Ram Baran Yadav had last week directed Parliament to begin a fresh process for the election of Prime Minister on the basis of majority vote as political parties had failed to hammer out a deal to form a consensus government.
Earlier Poudyal had failed to garner a majority in 16 rounds of voting, forcing him to quit the race.
The political crisis has been hugely damaging for Nepal, which is still reeling from its decade of civil war that ended in 2006.
Ready to discuss 2G on priority: Govt
I'm not at anyone's mercy, AP CM tells Jagan
Parliament's budget session to start on Feb 21
Britain set to ban Pakistani Taliban
Major cabinet reshuffle after budget session: PM