Fannie Mae earned $2.2 billion from April through June, its second quarterly gain in net income since being taken over by the government during the 2008 financial crisis.
The mortgage giant said Wednesday that it is paying $2.9 billion to the Treasury Department, its second straight quarterly dividend.
The company attributed the increase in earnings to improving home prices and fewer foreclosures.
Fannie's net income attributable to common shareholders was 37 cents per share in the second quarter. That compares with a net loss of $5.2 billion, or 90 cents per share, in the same period last year.
"We think home prices have stabilized," Fannie President and CEO Timothy Mayopoulos said in an interview on CNBC.
He said he believes the company can be profitable going forward, though that doesn't necessarily mean that Fannie will make enough money to pay a dividend each quarter to the Treasury.
"It's going to depend on home prices," Mayopoulos said.
Fannie and smaller sibling Freddie Mac were taken over by the government after massive losses on risky mortgages threatened to topple them.
Fannie has received about $116 billion so far from the Treasury Department, the most expensive bailout of a single company. So far Fannie has repaid about $26 billion of that bailout.
Taxpayers have spent about $170 billion to rescue Fannie and Freddie. It could cost roughly $260 billion more to support the companies through 2014 after subtracting dividend payments, according to the government.
On Tuesday, McLean, Va.-based Freddie reported net income of $1.2 billion, or 37 cents per share, for the second quarter and didn't request any additional federal aid for the period. The gain compared with a net loss of $3.76 billion, or $1.16 per share, in the same period a year ago.
The gains by the companies are the latest evidence of slow improvement in the housing market five years after the real estate bubble burst.
Fannie and Freddie own or guarantee about half of all U.S. mortgages, or nearly 31 million home loans, which are worth more than $5 trillion. Along with several federal agencies, they backed nearly 90 percent of new mortgages over the past year.
Fannie and Freddie buy home loans from banks and other lenders, package them into bonds with a guarantee against default, and then sell them to investors around the world.