Maryam Mirzakhani, an Iranian-born mathematician who was the first woman to win the coveted Fields Medal, has died in a US hospital after a battle with cancer. She was 40.

Mirzakhani' friend Firouz Naderi announced her death on Saturday on Instagram, and her relatives confirmed the death to the Mehr agency in Iran.

"A light was turned off today. It breaks my heart ... gone far too soon," wrote Naderi, a former director of Solar Systems Exploration at NASA.

READ MORE: Iranian the first woman to snare maths prize

"A genius? Yes. But also a daughter, a mother and a wife."

Mirzakhani, a professor at Stanford University in California, died after the cancer she had been battling for four years spread to her bone marrow, Iranian media said.

In 2014 Mirzakhani won the Fields Medal, the equivalent of the Nobel Prize for mathematics, which is awarded by the International Congress of Mathematicians.

The award recognised her sophisticated and highly original contributions to the fields of geometry and dynamical systems, particularly in understanding the symmetry of curved surfaces such as spheres.

Born in 1977 and raised in Tehran, she attended an all-girls high school and then studied at the capital's Sharif University.

Mirzakhani initially dreamed of becoming a writer, but by the time she started high school her affinity for solving mathematical problems and working on proofs had shifted her sights.

"It is fun - it's like solving a puzzle or connecting the dots in a detective case," she said when she won the Fields Medal.

A beautiful mind. A post shared by Firouz Naderi (@firouz_michael_naderi) onJul 15, 2017 at 8:14am PDT

"I felt that this was something I could do, and I wanted to pursue this path."

Mirzakhani said she enjoyed pure mathematics because of the elegance and longevity of the questions she studies.

"It is like being lost in a jungle and trying to use all the knowledge that you can gather to come up with some new tricks, and with some luck, you might find a way out," she added.

In 2008 she became a professor of mathematics at Stanford. She is survived by her husband, who is also a Stanford University maths researcher, and their young daughter.

'Great sorrow'

In Iran, President Hassan Rouhani said that Mirzakhani's "doleful passing" has caused "great sorrow," state media reported.

Rouhani praised the "unprecedented brilliance of this creative scientist and modest human being, who made Iran's name resonate in the world's scientific forums, (and) was a turning point in showing the great will of Iranian women and young people on the path towards reaching the peaks of glory...in various international arenas."

Rouhani also re-posted on Twitter a 2014 tribute with photos of Mirzakhani wearing a veil and without a veil.

تبریک به خانم مریم میرزاخانی. امروز ایرانیان میتوانند به خود ببالند که اولین زن برنده جایزه "فیلدز" هموطن آنان است. pic.twitter.com/xYhcWYcMu7 — حسن روحانی (@Rouhani_ir) August 13, 2014

Separately on Instagram, Iranian Foreign Minister Mohammad Javad Zarif said that Mirzakhani's death is a cause for grief for all Iranians.

Mirzakhani became known on the international mathematics scene as a teenager, winning gold medals at both the 1994 and 1995 International Math Olympiads -- and finished with a perfect score in the latter competition.

She went on to win the 2009 Blumenthal Award for the Advancement of Research in Pure Mathematics, and the 2013 Satter Prize of the American Mathematical Society.

Mirzakhani studied mathematics in Iran and earned a PhD degree from Harvard in 2004. She then taught at Princeton University before moving to Stanford in 2008.

The Fields Medal, which she won in 2014, is given out every four years, often to multiple winners aged 40 or younger.