SAN FRANCISCO (MarketWatch) -- Pervez Musharraf has agreed to resign as president of Pakistan to avoid being impeached by Parliament or prosecuted, according to media reports Thursday.

The controversial leader is expected to step down in the next few days, according to media reports Thursday.

Musharraf agreed to resign following secret negotiations between Musharraf's aides and the Pakistan's People's Party, the leader of the coalition that rules Parliament, The Wall Street Journal reported in its online edition, citing unnamed sources. See Wall Street Journal story

Part of the push for impeachment is thought to include Musharraf's declaration of a state of emergency in 2007, after which he was re-elected president, the Journal reported

Musharraf will leave in exchange for a guarantee that he will not be impeached or prosecuted, the Journal reported. Musharraf's intention to step down was also reported by other media organizations, including the New York Times.

His resignation would mark the end of a stormy administration that began when Musharraf led a military coup that overthrew Prime Minister Nawaz Sharif in October 1999.

Musharraf emerged as a key U.S. ally after the Sept. 11, 2001, terrorist attacks. Pakistan became a key area in the hunt for Al Qaeda and its leader, Osama bin Laden, who then were based in neighboring Afghanistan.

But Musharraf's administration, his military and spy service appeared to walk a tightrope between supporting the U.S. and cooperating with Al Qaeda and with the Taliban movement in Afghanistan.

Musharraf also became controversial for what many saw as a dangerous authoritarian streak. In 2002, he held a widely-criticized referendum that enabled him to secure support for another five-year term as president.

Later that year, a pro-Musharraf legislators won control of the Parliament in elections that observers said were flawed.

By late last year, Musharraf was facing growing opposition that only became stronger after Benazir Bhutto, leader of the opposition Pakistan People's Party, was assassinated in December 2007.

In February, the Pakistan People's Party, now led by her husband, Asif Ali Zardari, won the most seats in Parliament and formed a ruling coalition opposed to Musharraf. In April, the coalition announced its plan to impeach Musharraf.