Federal prosecutors reached an immunity deal with the tabloid executive David J. Pecker, a key witness in their monthslong investigation into payments during the 2016 campaign to two women who said they had affairs with Donald J. Trump, according to two people familiar with the investigation.

Mr. Pecker is the chairman of American Media Inc., the nation’s biggest tabloid news publisher, which was involved in the payments, which prosecutors have identified as illegal contributions made in violation of campaign finance law.

As prosecutors in New York built a case against Michael D. Cohen, Mr. Trump’s longtime lawyer, that resulted in a guilty plea on Tuesday, Mr. Pecker emerged as an important figure. The investigation appears to be continuing, and as a longtime friend and ally of Mr. Trump, Mr. Pecker could have additional information valuable to the prosecutors.

In pleading guilty to campaign finance violations, Mr. Cohen said Mr. Trump directed him to arrange the hush money payments to protect Mr. Trump from embarrassing stories during the campaign. The cooperation of Mr. Pecker is another potential blow to the president from a former loyalist.