Bank of America confirmed on Friday it agreed to acquire Countrywide Financial Corp. in an all-stock transaction worth approximately $4 billion, five months after making a $2 billion investment in the troubled mortgage lender.

In July last year, Bank of America purchased a 16 percent stake in the nation's largest mortgage lender during the height of the summer's global credit crunch.

The new agreement has been approved by the boards of directors of both companies and is subject to approval by Countrywide's shareholders and regulators, the bank said in a statement released on its website. The deal is expected to close in the third quarter.

Countrywide presents a rare opportunity for Bank of America to add what we believe is the best domestic mortgage platform at an attractive price and to affirm our position as the nation's premier lender to consumers, Kenneth D. Lewis, Bank of America's chairman and chief executive officer, said in a statement.

The Charlotte, North Carolina-based company will be sold at around $7.16 a share in stock. The offer is 7.6 percent below Countrywide's closing price yesterday on the New York Stock Exchange. Meanwhile, Countrywide shareholders will be issued 0.1822 of Bank America stock for each Countrywide share.

Countrywide stock fell 17 percent today at 10:40 a.m. to $6.42. Bank of America fell 90 cents, or 2.3 percent, to $38.40.

We believe this is the right decision for our shareholders, customers and employees, Angelo R. Mozilo, Countrywide's chairman and chief executive officer, said in the statement. Bank of America is one of the largest financial institutions in the U.S. and internationally, and we are confident that the combination of Countrywide and Bank of America will create one of the most powerful mortgage franchises in the world.

Countrywide helped fuel the housing boom by providing loans to high-risk borrowers. With the steep plunge in home prices last year, borrower defaults soared causing Countrywide's lending practices to come under close speculation from regulators ,legislators and consumer advocates.

The acquisition will make Bank of America the nation's largest mortgage lender and loan servicer.