Fannie Mae and its sibling, Freddie Mac, have been pushing banks to buy back loans they sold to the two companies that never should have been sold to them because the loans did not meet the companies' criteria for purchasing.

Bank of America said most of the settlement would be covered by reserves, and another $2.5 billion, before taxes, that it set aside in the fourth quarter.

A separate settlement over foreclosure delays will result in Bank of America paying $1.3 billion to Fannie Mae, the mortgage company said. Bank of America had already set aside money to cover most of that, but took another $260 million charge in the fourth quarter to cover the balance.

Bank of America also sold the rights to collect payments on about $306 billion of loans to Nationstar Mortgage Holdings and Walter Investment Management. Nationstar is paying $1.3 billion for the right to service some $215 billion of loans, while Walter Investment is paying $519 million for the right to service about $93 billion of mortgages.

Reuters first reported that Bank of America was talking to Nationstar and Walter Investment on Friday.

After the announcement, shares of Bank of America traded higher in pre-market trading.