Pakistan has two competing parallel powers, the Pakistan Government and the Pakistan Military. I have seen that the government wants to improve relations, increase business investment, save the economy from bankruptcy, reduce all unnecessary spending etc. I think this is the best government Pakistan have had in decades.
The government is trying to save every rupee even to the extent of auctioning off cars and buffalos owned by the government. The next logical step would be to reduce the military budget. That is an unproductive spending which every government will eye to reduce this budget.
However, the Military also knows this and doesn't want to cut down their salaries or their size. They know that if their budget is cut, then they will become a smaller power and will likely come under the government. They can't allow that to happen. One way to do this is to make everyone realize the need for a strong military.
They know that allowing such a terrorist attack to happen will lead to all Indians and Iranians screaming for a retaliation. And, in such a situation, the Pakistani government cannot reduce the military budget, but will most likely instead increase it. This is not a situation exclusive to Pakistan, but such things have happened many many times in history whenever the military and the government are competing powers.
I think this is the main reason why this attack happened.
The Pakistani military is not like any other military where it is controlled by the elected government, instead, this military is controlled by a set of an elite group of people. These people have involved in a lot of crime and corruption, but they are untouchable because they are protected by the military. Most of them are anyway high ranking military officials.
The government of Pakistan is the secondary power which they are allowing to exist as a face of the country because they know the country will fall apart if there is no such facade like how it happened in almost all countries controlled by the military. The government is given near full freedom to do whatever they want EXCEPT interfering the activities of the military or its controlling elite.
Which country's military runs businesses and corporations? Pakistan's military does so! They are into every kindof business in Pakistan. Infact, just like how Tata and Reliance are the biggest business empires of India, the Pakistan Army is the biggest business empire of Pakistan.
The military owns so many businesses because they want to decrease the dependence on the funding from the government. However, they are still critically dependent on the government's money, so they cannot allow the government to reduce that budget.
The best definition of Pakistan army which people can understand is - Imagine if a conglomerate corporation of your country was the Army and the civilian government was paying money to them for "security". But, then a new government came and wants to reduce its budget because the country is in an economic crisis.
So, just causing a terrorist attack in India and Iran will cause people of both countries to rally up anger towards the Pakistani government. This will scare the government and the people. This will make it a very very unpopular move for the government to even talk against the military. The civilians will infact bring down the government if they talk the truth or try to reduce the military budget.
This is how the military wins and stays in power. This is the reason why whenever the government of Pakistan reaches a near-peace-treaty with India, the military get scared and causes a terrorist attack to make the treaty fall apart.