At least 24 people have died after a suicide bomber targeted a train carrying military personnel and their families in Pakistan, officials say.