A three-judge bench headed by Chief Justice Iftikhar Chaudhry also said that any "political cell" operating in the presidency, ISI, Military Intelligence or Intelligence Bureau should be shut down immediately as such an institution is "unconstitutional".
Any notification to create such a cell would be null and void, the bench ruled.
It further directed the Federal Investigation Agency to probe the distribution of funds among politicians so that legal action could be taken against them.
The apex court issued the directives in response to a petition filed in 1996 by former air force chief Asghar Khan against the distribution of funds by the ISI among politicians to prevent the Pakistan People's Party's victory in the 1990 polls.
The apex court resumed hearing the petition earlier this year after a gap of over 12 years.
While referring to the role played by Beg and Durrani, the bench said they had violated the constitution and the law by meddling in politics.
Their actions had affected the image of the country and the armed forces, it said.
Though the two generals were retired, the court directed the government to take action against them according to the constitution and the law.
The bench said the late President Ghulam Ishaq Khan had created an "election cell" in the presidency which had rigged the 1990 polls and polluted the political system.
Both the presidency and the army chief cannot create such cells or interfere in the political process, it said.
During the hearing of the case, the apex court was informed that the "election cell" in the presidency had received Rs 140 million from banker Younis Habib for rigging the polls.
Of this, Rs 60 million was distributed among politicians while Rs 80 million was retained in a "survey and construction account" maintained by the Military Intelligence.
The Supreme Court directed authorities to recover the amount paid to politicians with interest and to deposit the funds with the national exchequer.
The amount retained by the Military Intelligence too should be recovered. Action should also be taken against banker Younis Habib, the court said.
The court's order made it clear that secret agencies have no role in the election process and the military cannot participate in politics or destabilise the democratic government.
The chief justice read out the bench's short order after reserving its verdict earlier in the day.
Asghar Khan, who is now affiliated with cricketer-turned-politician Imran Khan's party, described the order as a "correct decision".
Khan's lawyer Salman Akram Raja said the apex court had specifically named Beg and Durrani for violating the constitution and directed authorities to probe all politicians linked to the scandal.
The election commission could also act against the politicians, he said.
"For the first time in Pakistan's history, a clear decision has been made by the court against officers of the army.
"It is the first step towards ending the imbalance of institutions that has existed for over 60 years," Raja said.
There is no political cell within ISI: Pak govt tells SC
Nobody knows how ISI's political cell was created
PIX: Biden, Ryan spar over Iran in US vice prez debate
REVEALED: How ISI-Naxal ties were forged
ISI has links with Naxals: West Bengal DGP