LONDON — After Taliban gunmen massacred dozens of schoolchildren in Peshawar last month, Pakistan’s two most powerful men convened an emergency meeting at army headquarters. Their body language, captured in a government-released photo, was revealing: Prime Minister Nawaz Sharif looked glum and ill at ease, while the man beside him, Gen. Raheel Sharif, the army chief, lectured confidently.

To many Pakistanis, the symbolism was rich and unambiguous. After a tumultuous year, Mr. Sharif’s government may still be hanging on, extending a nearly seven-year stretch of civilian rule. But otherwise, Pakistan’s generals are back in the driver’s seat.

Under General Sharif, who took his post in late 2013 and is not related to the prime minister, the army has transformed its fortunes: triumphing over the government in a series of bruising public clashes, bringing unruly critics in the news media to heel, and winning broad support for a drive against Islamist militants in their tribal stronghold.

Now, the military has claimed a victory that may turn out to be the most significant of all, allowing the generals deep inroads into an institution that has hounded them in recent years: Pakistan’s judiciary.