At least 12 soldiers and six Islamist militants have been killed after a suicide bombing in northwestern Pakistan, the country's military has said.