Ending years of legal squabbling over soured mortgages, Bank of America (BAC) agreed on Monday to hand over $3.6 billion to Fannie Mae and buy back $6.75 billion in mortgages to settle claims tied to a nine-year period.

Fannie Mae had been demanding that BofA take financial responsibility for mortgage loans that the agency believed failed to meet adequate underwriting standards.

At the same time, BofA said it will unload approximately $306 billion of residential mortgage servicing rights to Nationstar Mortgage Holding (NSM) and Walter Investment Management (WAC).

The agreements should remove some of the uncertainty still hovering over BofA in the wake of the financial crisis, subprime bust and ill-advised acquisition of mortgage giant Countrywide Financial in 2008.

Shares of the Charlotte-based lender rose modestly Monday morning to fresh 52-week highs.

“These agreements are a significant step in resolving our remaining legacy mortgage issues, further streamlining and simplifying the company and reducing expenses over time,” BofA CEO Brian Moynihan said in a statement.

BofA said it has now addressed “substantially all of its remaining exposure” to buyback obligations for residential mortgages sold to Fannie Mae.

Still, the settlements will cost shareholders as BofA said it will take a $2.7 billion hit to fourth-quarter net income, leaving the company just “modestly” in the black for the quarter.

Fannie Mae requires its mortgage seller and servicers to buy back loans or foreclosed properties or reimburse it for loans that don’t meet underwriting, eligibility or other requirements in place.

BofA agreed to make a cash payment of $3.6 billion to Fannie Mae and repurchase $6.75 billion of certain residential mortgage loans that were sold to the government-controlled mortgage giant between 2000 and 2008 by both BofA and Countrywide.

The loans covered in the deal have an aggregate original principal balance of about $1.4 trillion and an aggregate outstanding principal balance of around $300 billion. BofA has valued the loans at less than the purchase price.

At the same time, BofA said it has agreed to make a cash payment to Fannie Mae to settle substantially all of its outstanding and future claims for compensatory fees stemming from past foreclosure delays. These payments are expected to be covered by existing reserves and an additional fourth-quarter charge of about $260 million.

In a sign of reduced legal uncertainty, BofA said it expects to cut its range of possible loss above existing accruals for both GSE and non-GSE representations and warranties exposure to up to $4 billion from up to $6 billion at the end of September.

BofA also announced a deal to sell residential mortgage loans serviced for Fannie Mae, Freddie Mac, Ginnie Mae and private labels that have an aggregate unpaid principal balance of about $306 billion.

These loans include about 232,000 that are classified as more than 60 days delinquent. The transfers of servicing rights are scheduled to take place throughout the year.

"We are resolving legacy mortgage issues while balancing the needs of our customers, mortgage investors, our shareholders and communities,” said Ron Sturzenegger, legacy asset servicing executive at BofA.

BofA said it expects its earnings per share to be “modestly positive” for the fourth quarter after taking into account the slew of mortgage-related charges.

Shares of BofA, the No. 2 U.S. bank by assets, gained 0.58% to $12.17 Monday morning, leaving them up about 96% over the past 12 months.

In addition to the settlement, BofA’s shares received a boost from S&P Capital IQ, which upgraded BofA to “hold” from “sell” and hiked its price target to $13 from $8.

Follow Matt Egan on Twitter @MattMEgan5