Bank of America Corporation (BofA), the second-biggest U.S. bank by assets, posted a first-quarter profit Wednesday, narrowly beating Wall Street expectations, as its legal costs fell sharply and the bank earned more from mortgage lending and investment and brokerage services. BofA reported profit of $2.98 billion, compared with a surprising loss of $514 million in the same period a year ago, attributable to legal costs from lawsuits related to the 2008 financial crisis. Revenue fell 5.9 percent to $21.42 billion, in contrast to revenue gains reported Tuesday by rivals J.P. Morgan Chase and Wells Fargo. Revenue from investment and brokerage services rose 3.3 percent to $3.38 billion, while revenue from fixed-income, currencies, and commodities trading fell 6.8 percent to $2.75 billion. Revenue from foreign-exchange sales and trading doubled, while mortgage-banking revenue rose 68 percent to $694 million.