Mattel has posted a surprise fourth-quarter profit, as the toymaker benefited from a makeover of its iconic Barbie doll, while keeping a tight lid on costs.

This sent its shares soaring 19% in extended trading on Wall Street. 

Barbie ended the year with its highest gross sales in five years, Mattel said, highlighting the company's success in diversifying the traditionally blonde doll by giving it different skin tones and dressing it in attires ranging from hijabs to space suits. 

The Barbie Dreamhouse was among the top ten best-selling toys in the US in 2018, according to market research firm NPD group. 

Gross sales rose 12% for Barbie and 9% for Hot Wheels in the fourth quarter, handily beating analysts' estimates for both brands. 

To tap into the renewed popularity of the doll, Mattel has partnered with Warner Bros to make a live-action feature movie on Barbie, starring Oscar-nominated actress Margot Robbie. 

The company also benefited from its plans to streamline its operations, with costs falling 27% to $814.7m in the quarter. 

Mattel has been aiming to cut at least $650m in net costs by the end of 2019 through job cuts and other measures such as looking at options for its manufacturing facilities. 

"Restoring profitability for the company this year has been the core focus," chief executive Ynon Kreiz told Reuters, adding that he expects Mattel to exceed its cost-saving target. 

Mattel reported net income of $14.9m, or four cents per share, in the quarter ended December 31, compared with a loss of $281.3m, or 82 cents per share, a year earlier, when it took a one-time charge related to changes in US tax laws. 

Analysts had expected a loss of 16 cents per share, according to IBES data from Refinitiv.

However, gross sales in North America fell 10.1% to $744.5m, reflecting Mattel's struggles with finding a major retailer to make up for lost sales from the shuttering of Toys 'R' Us. 

Overall, Mattel's net revenue fell 5.4% to $1.52 billion, but was still above analysts' estimates of $1.44 billion.