Bank of America Corp. is set to pay a record settlement of nearly $17 billion over its mortgage lending as early as Thursday, according to people familiar with the matter, capping a legal odyssey that has dogged it since the depths of the financial crisis.

The deal will resolve a government investigation that stems largely from the bank's purchases of Merrill Lynch & Co. and Countrywide Financial Corp. as they teetered in the housing crisis. More than $1 billion of the payout from the Charlotte, N.C., bank could go to...