A former National Security Agency contractor was sentenced Friday to nine years in prison after pleading guilty earlier this year to removing classified information from the spy agency, in what is widely viewed as one of the largest thefts of U.S. government secrets in history.

Harold “Hal” Martin III, who was indicted in 2017 and accused of taking home thousands of pages of documents containing some of the nation’s most sensitive secrets over a span of several years, pleaded guilty in March to a single count of willful retention...