It pointed out that in the three days of court proceedings, the government kept changing its stance, interchangeably "referring to it as reappointment, limiting of retirement or extension of tenure".
The verdict was announced after the government submitted a "new summary" to extend the service of 59-year-old Gen Bajwa.
The ruling comes in the nick of time as he was set to retire at midnight Thursday.
A much-relieved Prime Minister Khan lauded the apex court after the ruling even as he took a jibe at the country's "external enemies" and the "mafias within".
Khan had extended Bajwa's tenure through a notification on August 19, but the apex court suspended it on Tuesday, citing irregularities in the manner the army chief, a close confidant of the prime minister, was granted an extension.
"Today must be a great disappointment to those who expected the country to be destabilised by a clash of institutions," Khan tweeted.
"That this did not happen must be of special disappointment to our external enemies & mafias within," Khan said, without identifying them.
Delivering the verdict, Chief Justice Khosa directed the government to bring necessary legislation within six months.
He said the court observed "judicial restraint" and left the matter to the Parliament.
"We have reviewed several laws including Army Act 1952 and Rule 1954 before announcing the judgment," he said.
The bench had concluded the case in the morning after three consecutive days of hearing but reserved the judgment.
It had directed to the government to present a new "summary" for extension in the service of Gen Bajwa. The bench had also directed that the summary should not mention any time period for which the service of army chief would be extended.
Earlier, when the hearing restarted, Attorney General Anwar Mansoor Khan told the court that government made a fresh appointment of Gen Bajwa as army chief under article 243 of the Constitution.
However, the court questioned how the fresh appointment was different from the previous appointment and the government's legal team failed to satisfy the court.
The bench asked the government to produce the documents related to the extension and pension of former Army chief Gen Ashfaq Pervaiz Kayani and those related to the retirement of his successor General Raheel Sharif.
As the documents were not readily available, the court suspended the proceedings for 15 minutes. When the hearing resumed the relevant document were presented. But even those papers failed to answer the basic question that under which law the government was giving extension to the Army chief for three years.
It turned out that there was no law on the issue of extension and reappointment of a sitting Army chief for three years. Khosa asked if the government was ready to formulate laws in specific period.
Attorney General Khan said the government can enact laws in three months. It prompted the court to offer that it can grant extension for three months to the army chief.
However, lawyer of the Army chief, Farogh Naeem who resigned from his post as law minister on Tuesday to pursue the case, said the government would make laws within six months.
Khosa finally ordered that the government should come up with the new summary to appoint an Army chief and after seeing it the court would issue its verdict. He ordered that the salary and other perks of the army chief should also be mentioned in it.
Some interesting exchanges took place during the hearing as Chief Justice Khosa said that judges were termed as "agents of India" and "agents of CIA" for questioning laws related to the extension of the Army chief.
He said that it was also alleged that the top court proceedings were part of "fifth generation warfare" against Pakistan.
The top judge said that the objective of the court was to streamline the procedure for the key appointment.
The case started unfolding on Tuesday when the court suspended the government notification of August 19, granting a three-year extension to Gen Bajwa. The court said the notification was not valid on technical and legal grounds.
The government then withdrew the August 19 order after the apex court observations and issued a fresh notification which was also rejected by the court on Wednesday.
The unprecedented action by the apex court shook the high echelons of power as the Imran Khan government scrambled to control the damage.
The powerful military, which has ruled Pakistan for more than half of its 70 plus years of existence, has wielded considerable power in deciding matters concerning security and foreign policies.
When Bajwa was appointed Army chief by former prime minister Nawaz Sharif, he superseded three generals.
Business Standard has always strived hard to provide up-to-date information and commentary on developments that are of interest to you and have wider political and economic implications for the country and the world. Your encouragement and constant feedback on how to improve our offering have only made our resolve and commitment to these ideals stronger. Even during these difficult times arising out of Covid-19, we continue to remain committed to keeping you informed and updated with credible news, authoritative views and incisive commentary on topical issues of relevance.
We, however, have a request.
As we battle the economic impact of the pandemic, we need your support even more, so that we can continue to offer you more quality content. Our subscription model has seen an encouraging response from many of you, who have subscribed to our online content. More subscription to our online content can only help us achieve the goals of offering you even better and more relevant content. We believe in free, fair and credible journalism. Your support through more subscriptions can help us practise the journalism to which we are committed.
Support quality journalism and subscribe to Business Standard.