Lt Arsalan Alam was posted in Rajgal, Khyber Agency, Inter-Services Public Relations, the media wing of the Pakistan military, said in a statement.
"Terrorists fired from across the border on a newly established Pakistani border post in Rajgal, Khyber Agency," it said.
Lt Alam, the officer commanding that post, got hit while performing his duties and died, the statement added.
Relations between Pakistan and Afghanistan have been tense for months, with the two sides accusing each other of turning a "blind eye" towards certain militant outfits.