AIG, the giant insurer bailed out by the U.S. government, posted its first profit in seven quarters on Friday, helped by large investment gains as financial markets improved, sending its shares up 18 percent.

The company, which has received $180 billion of federal aid and is now 80 percent-owned by U.S. taxpayers, also reported strides in unwinding AIG Financial Products, the unit responsible for nearly half of the parent's $99 billion in losses last year.

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

Chief Executive Ed Liddy, on his last day at AIG, warned there could still be swings in future results, particularly from accounting charges stemming from its restructuring. AIG is winding down some businesses and selling off others to cut exposure to risky investments and raise cash to repay more than $80 billion in taxpayer loans.

Despite a steep rally in AIG shares this week ahead of the results, Standard & Poor's stock analyst Catherine Seifert said the company remains a risky bet.

Results were better than we expected and reflected positive mark-to-market gains, not strength in underlying operations, she said in a research note. We believe that the turnaround in Q2 book value may not be sustainable, and that the risk/reward for common equity holders is not attractive,

AIG reported a second-quarter net profit of $1.8 billion, or $2.30 a share, its first profit since the third quarter of 2007. In the year-earlier second quarter it reported a net loss of $5.4 billion, or $41.13 a share.

After dividends on preferred stock held by the government, profit attributable to common shareholders was $311 million.

Adjusted profit, which excludes realized capital gains and losses, was $2.57 a share, far above the average forecast of $1.33 from four analysts polled by Reuters Estimates.

AIG shares were up 18 percent at $26.61 in midday trade on the New York Stock Exchange. The shares rose 70 percent over the previous four trading days in anticipation of improved results, forcing short sellers to become buyers to cover their positions.

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.

The cost of insuring $10 million of AIG debt against default for five years dropped on Friday to an upfront payment of $1.65 million plus $500,000 annually, according to Markit Intraday. On Thursday the upfront payment was $2.2 million.

BABY STEPS

Once the world's largest insurer, AIG nearly collapsed from credit default swaps that left it on the hook for tens of billions of dollars in payouts to some of the biggest U.S. and European banks.

The U.S. government saved AIG because its failure was deemed to be a threat to the stability of global financial markets. It is unclear if AIG will ever be strong enough for the government to relinquish its stake.

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

That helped push the value of AIG's total equity up nearly 17 percent to $62.1 billion as of June 30.

Liddy said significant progress had been made, but warned that paying back taxpayers and restoring long-term financial viability was highly dependent on market conditions.

Liddy, who earned $1 a year, is leaving AIG after 11 tumultuous months of trying to set the company on a path to recovery. 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.

In the second quarter, AIG's financial products unit cut the size of its derivatives portfolio by 13 percent to $1.3 trillion.

So far this year, small asset sales by AIG have generated $2.6 billion to pay down borrowings from the Federal Reserve.

A pending government agreement involving AIG's largest life insurance units is expected to cut its indebtedness by another $25 billion.

But weakness in its insurance operations may not bode well for its plans to sell large stakes in some of its biggest operations to outside investors, or through public offerings.

AIG's general insurance operations posted a 19 percent decline in net written premiums in the quarter to $7.9 billion. Premiums for its life insurance and retirement services division fell 15 percent to $8.1 billion. (Reporting by Lilla Zuill; Additional reporting by Juan Lagorio and Karen Brettell; Editing by Derek Caney and John Wallace)