Soaring interest rates continued to eat away at the earnings of the Federal National Mortgage Association in the second quarter, giving the giant secondary-market lender a net loss of $18.5 million, Fannie Mae reported yesterday.

Borrowing costs have increased more than the yield on mortgages, the corporation said.

The latest report was a slight improvement over that of the first quarter, when the federally chartered corporation had a net loss of $21.6 million; in the second quarter of last year, the loss amounted to $16.5 million.

Fannie Mae said that the latest loss was held down by a previously announced reduction in its reserve for conventional loan losses and a decrease in the provision for estimated losses on conventional loan purchases. Excluding those reductions, the second-quarter loss would have been $40.9 million.

For the first six months, FNMA's net loss was $40.1 million compared with net income of $7.6 million (12 cents a share) a year earlier.

Fannie Mae reported a $53.8 million portfolio loss for the second quarter compared with a loss of $49.2 million in the first quarter and $42.3 million in the second quarter of 1980.

But at the same time in this latest quarter, commitment fee income -- from charges paid by lenders when Fannie Mae agrees to buy their loans -- climbed to $16.6 million from $6.3 million in the first quarter. A spokeswoman said demand for Fannie Mae commitments rose because of new programs instituted by the secondary-market corporation and because deposit flows to thrift institutions have been down in recent months.

She said Fannie Mae was taking "every action to remedy" its drop in earnings through such programs as its buy-down and loan participation purchase offers, a resale refinance plan, which recycles old low-yielding mortgages into higher-yielding new loans, and its newly announced plan to buy adjustable-rate mortgages.

First Virginia Banks Inc. reported that first-half income before securities transactions was $9.276 million (85 cents a share) compared with $7.041 (67 cents) during the same period a year before.

The bank attributed the higher earnings to a greater volume of earning assets and to higher interest rates. Earnings for the first half of 1981 were equal to 1.17 percent of average total assets and 17.13 percent of average stockholders' equity.

Net income, including securities transactions, was $9.275 million (85 cents) compared with $7.069 million (67 cents) a year before. Total assets were $1.63 billion as of June 30. Total deposits were $1.44 billion compared with $963.5 million a year before.