Health insurers made oodles of money in 2016, despite losing millions of dollars on Obamacare.

The industry's net income rose to $13.1 billion, up 46% from a year earlier, according to a report from rating company A.M. Best.

Companies saw improvements in most of their business lines, but much of the earnings boost came from higher profitability in their Medicare Advantage divisions and smaller losses in their Obamacare exchange products.

"They did lose millions and millions" in Obamacare, said Doniella Pliss, associate director at A.M. Best. But "it doesn't contradict the fact that they did very well in other lines of business."

Related: Aetna reverses course, files to offer Obamacare policies in Nevada

Insurers reported an underwriting loss -- when claims paid exceed premiums collected -- of $893 million in 2016, down from $2.1 billion a year earlier, in their commercial business lines, which includes Obamacare policies. Insurers were able to stem the bleeding by increasing rates, restricting provider networks and changing benefits packages. For instance, some insurers are reining in costs by restricting out-of-network access and monitoring patients more closely.

Obamacare "is not eating into profits to the degree that it did in the prior year," Pliss said.

Related: Anthem pulls out of Obamacare in Ohio for 2018, citing uncertainty

Where insurers did well was Medicare Advantage, where underwriting profitability soared 279%. Under Medicare Advantage, the federal government pays companies a fixed amount each month to cover Medicare-eligible Americans. Insurers are benefiting from an influx of younger, healthier retirees, better management of enrollees' health and more favorable contracts with providers.

Medicaid, however, has been a more challenging area for carriers. Their underwriting earnings declined in this segment, mainly because new Medicaid expansion enrollees have been sicker. However, this business line should improve over time as consumers have better and more consistent access to care.