Bank of America Corp., the second-biggest U.S. lender, swung to a surprise loss as the company booked $6 billion of costs tied to mortgage disputes.

The first-quarter loss was $276 million, or 5 cents a diluted share, compared with a profit of $1.48 billion, or 10 cents, a year earlier, according to a statement Wednesday from the Charlotte, N.C.-based firm. Adjusted earnings were 35 cents a share, beating the 27-cent average estimate of 12 analysts surveyed by Bloomberg.