Bank of America beat most analysts’ expectations in the third quarter, posting a modest profit when most on the Street had forecast more red ink.

The bank, led by Chief Executive Officer Moynihan, posted the $168 million net profit despite agreeing to pay the nearly $17 billion settlement, the largest ever, to federal and state authorities to settle a probe into its toxic mortgage-backed securities.

The small profit in the three months ended Sept. 30 is equal to a loss of 1 cent a share after payment of dividends on preferred shares, according to third-quarter results released Wednesday morning.

Wall Street was expecting a loss of 9 cents.

A year ago, BofA reported a profit of $2.5 billion.

In the most recent quarter, the profit reflects litigation charges of $5.6 billion.

Revenue in the period fell 1.5 percent to $21.21 billion — below expectations.

Shares in pre-market trading were down 1.4 percent, to $16.29.

The surprise small net profit is the result, in part, of an 11 percent increase in bond, currency and commodity trading revenue, the bank said in a statement.

JPMorgan Chase and Citigroup both released profitable third-quarter results on Tuesday — also underpinned by better-than-expected trading gains.