There are territorial waters entering which is an obvious declaration of war. Territorial waters are about 12 nautical miles from the shore. Then there are economic zone (EEZ) where a country can undertake fishing and drilling activities, other nation's ships can pass through here, naval ships passing through EEZ have to be in touch with naval services and show that they are not hostile, subs have to surface while passing through EEZ.
Pakistan's EEZ extends 80 nautical miles from the shore and reports are this sub was detected around 40 NM from the shore (so not in territorial waters, but in economic zone). The sub was detected by airborne sub detectors and locked on by anti-sub helicopters. Upon realizing that it has been detected the Indian sub surfaced (which is a declaration of surrender in submarine warfare) and it was then escorted out of Pakistani waters by Pakistani naval ships.
Obviously India denies all of this even though Pakistan has provided photographic and video evidence.