American International Group Inc , the insurer that received $180 billion of federal bailouts, posted its first profit in seven quarters on Friday, helped by investment gains, sending its shares up 20 percent in premarket trade.

Improving market conditions boosted the value of securities AIG had previously written down, and earnings easily topped Wall Street expectations. The company also reported further progress in unwinding AIG Financial Products, the unit responsible for nearly half of its $99 billion in losses last year.

But its insurance operations were weak, underscoring the wear and tear inflicted on its main business by the company's financial woes.

Second-quarter net earnings were $1.8 billion, or $2.30 a share, compared with a net loss of $5.4 billion, or $41.13 a share, a year earlier.

It was the company's first quarterly profit since the third quarter of 2007.

The reality is that the numbers are getting better and as a result it could be viewed as a potentially constructive first step toward financial success, said Michael Holland, a money manager with Holland & Co.

After dividends on preferred stock held by the federal government, profit attributable to common shareholders was $311 million. Taxpayers own nearly 80 percent of AIG as a result of the bailout.

On an adjusted basis, the company earned $2.57 a share, far above the average forecast of $1.33 among four analysts polled by Reuters Estimates. Adjusted earnings exclude realized capital gains and losses.

AIG shares were up 20 percent to $27.04 in premarket trading. The shares had risen 70 percent since Monday as investors bought in anticipation of improved second-quarter results, forcing short sellers to also become buyers to cover their positions.

Once the world's largest insurer, AIG nearly collapsed last year because of its exposure to credit default swaps, which left it on the hook for tens of billions of dollars of payouts.

The latest results included $5.7 billion in unrealized gains on investments from improving market conditions and the adoption of new accounting rules.

Our results reflect stabilization in certain of our businesses, Chief Executive Ed Liddy said in a statement. But other operations, including its main insurance business, remained challenged, largely driven by weak economic conditions and the lingering effect of the negative AIG events earlier in the year, he said.

In the quarter, AIG's financial products unit cut the size of its derivatives portfolio by 13 percent to $1.3 trillion. The portfolio has been cut 17 percent so far this year.

The company's general insurance operations posted operating income, which excludes net realized capital gains, of $1 billion, down from $1.7 billion a year earlier.

Net written premiums fell 19 percent to $7.9 billion.

Liddy is leaving AIG after 11 tumultuous months trying to set the company on a path to repay more than $80 billion in taxpayer loans. On Monday, Robert Benmosche, a former MetLife Inc executive, will take over as AIG CEO, and former American Express Co chief Harvey Golub will become AIG chairman.

(Reporting by Lilla Zuill; Additional reporting by Juan Lagorio; Editing by Derek Caney and John Wallace)