Zambia's high court has declared that Defence Minister Edgar Lungu is the governing party's presidential candidate in next month's election.
The Patriotic Front (PF) party has been split over who will succeed President Michael Sata following his death.
One faction chose Mr Lungu as the party's candidate while another opted for economist Miles Sampa.
The split could open the way for the opposition to take power in the elections, analysts say.
The rival PF factions held separate conferences over the past week to elect a leader and presidential candidate for the 20 January election.
Mr Lungu's faction challenged Mr Sampa's nomination in court.
"The purported general conference of 1 December at which the said honourable Miles Sampa was elected as another party president of the Patriotic Front be declared illegal, null and void and all decisions made thereof are quashed," the judge ruled.
Analysis: BBC Africa's Chakuchanya Harawa
Zambian President Michael Sata gestures upon arrival at Solwezi airport before addressing supporters at an election campaign meeting on 10 September 2014
Mr Sata had a firm grip on the PF after its formation in 2001. Following his death, it has been hit by a leadership conflict which threatens to tear it apart.
And this is happening less than three weeks before the party has to submit the name of its presidential candidate to the Zambia Electoral Commission.
Its rules state that candidates for the January election must be sponsored by a political party; independents are not allowed. But rival factions have chosen their own candidates and have turned to the courts to legitimise their choice.
The feuding has left a huge dent on the PF and it could cost the party the election. The eventual winner in this saga will have very little time to unite the party and convince everyone to rally around him. Other political parties have already launched their campaigns; the PF will have to play catch up.
Mr Sampa said he would appeal against the ruling.
He is allied to Zambia's interim President Guy Scott, who took office after Mr Sata died in a UK hospital in October at the age of 77.
Mr Scott has played a key role in the power-struggle, suspending Mr Lungu as PF secretary-general shortly after Mr Sata's death.
He was forced to reinstate Mr Lungu a day later following an outcry in the party.
Mr Scott, who is ineligible to run for the presidency, is trying to play the role of kingmaker, some analysts say.