A four-judge bench of the Peshawar High Court headed by Chief Justice Dost Muhammad Khan imposed the ban while dismissing Musharraf's appeal challenging the rejection of his nomination papers for May 11 general election.
The lifetime ban was imposed because Musharraf had abrogated the Constitution twice and detained judges during the 2007 emergency, the bench said.
Musharraf was barred from contesting polls to the national and provincial assemblies and the Senate, it said.
One of Musharraf's lawyers, Saad Shibli, says he would challenge the ruling in the Supreme Court.
Election authorities had earlier rejected Musharraf's nomination papers for four parliamentary seats in Islamabad, Punjab, Sindh and Khyber-Pakhtunkhwa because of his actions while in power.
Musharraf's lawyers had filed an appeal in the Peshawar High Court against the rejection of nomination papers for a parliamentary seat in Chitral region of Khyber-Pakhtunkhwa province.
The 69-year-old former President is currently being held at his farmhouse on the outskirts of Islamabad, which has been declared a sub-jail, after he was arrested over the sacking of judges during the 2007 emergency and the assassination of former premier Benazir Bhutto.
An anti-terrorism court in Rawalpindi today remanded Musharraf to judicial custody for a fortnight over the Bhutto assassination case.
He has been accused of providing inadequate security to Bhutto after she returned to Pakistan from self-exile in 2007.
Musharraf, who heads the All Pakistan Muslim League, returned to Pakistan last month after nearly four years in self-exile to make a political comeback but he has been dragged to court over several issues, including the imposition of emergency rule in 2007 and the death of Baloch leader Akbar Bugti in a 2006 military operation.