In the land of the battered bank, just scratching out a profit can make you king.

That’s turning out to be the story with Morgan Stanley as it gained ground on rival Goldman Sachs in its battle for supremacy on Wall Street — at least the portion not occupied by vagrant protesters.

The white-shoe firm run by James Gorman yesterday posted a third-quarter profit of $2.2 billion, or $1.15 a share — thanks to a $3.4 billion accounting gain.

That beat Wall Street estimates — and easily outpaced rival Goldman, which posted a loss of $438 million for the same period.

Morgan Stanley shares, which had been beaten up over concern about the firm’s exposure to European banks, gained 1 cent yesterday, to $16.64.

Both iconic Wall Street franchises are confronting the stiffest headwinds they have faced since the credit crisis in 2008.

And so far Morgan Stanley, which has operated in the shadows of its more prominent rival, is starting to make headway.

Morgan Stanley’s $6.5 billion in revenues were more than twice the $3.14 billion that Goldman generated, excluding $450 million in gains for its debt valuations.

To be sure, Morgan benefits from its 17,000-plus army of brokers.

Gorman, 53, has hitched much of the firm’s fortunes to the success of its brokerage business Morgan Stanley Smith Barney.

But even in investment banking Morgan Stanley appeared to best its rival.

Morgan Stanley posted $864 million in revenues from its investment banking unit, compared to $781 million for Goldman.

Gorman and his team yesterday tried to beat back rumors about its exposure to the embattled economies of Europe. Morgan Stanley said that its exposure to European debt was $5.7 billion, or about $2.1 billion on a hedged basis.