She was born to lieutenant-colonel Johan Stålhammar, himself a veteran of the war, but who became almost ruined after his retirement in 1702. When he died in 1711, he left six daughters without money, and they had to rely on the charity of relatives and marry people whom they considered to be below their standards to support themselves. Ulrika did not wish to marry, and one night, she dressed herself in her father's clothes, stole a horse from the stable and ran away from home.
She enlisted in the army in Kalmar as an artillerist in 1713, under the name of William Edstedt, and remained in the army for thirteen years. In 1716, she fell in love with a maid named Maria Lönnman and married her. Her wife at first thought that she was impotent, but was content to live without sex (the reason given by some sources is that Maria had earlier been a victim of rape). Ulrika eventually revealed her sex, and they continued to live happily in what was later described as a union of "spiritual love".
In 1724, her sister Catherine learned of what she had done. Shocked over both the crossdressing and the marriage, she wrote to Ulrika that she had committed a "sin against the will of God". Ulrika then promised to leave the army, but she did not do this for two years. She and Maria sought refuge with her wealthy aunt, Sophia Drake, and asked for her protection. Ulrika hid out in the country for two years, so that she could gradually get used to wearing women's clothing again, while Maria worked as a housekeeper at her aunt's mansion.
In 1728, Ulrika went to Denmark and wrote a letter of confession to the Swedish government and asked for its pardon. She then returned to Sweden where she was put on trial. The judges could find nothing in the law to stipulate what punishment she should have, but agreed she should have some punishment, and after consulting the Bible, charged her with having "violated the order of God" by dressing as a man, and with "making a mockery of marriage" by marrying a member of the same sex.
The judges were curious how she had managed to pass for a male, and had a midwife examine her physically. The midwife reported that she was completely normally developed, though she "lacked breasts".
However, the judges were also impressed and intrigued with her; she was from Småland, which reminded people of the legendary female warrior Blenda, who was also from Småland, and her homosexuality was excused by the testimony of her wife Maria, who swore that they never had any sexual contact. This caused the judges to view her marriage as a thing in her favour, as it was "of the purest, most spiritual kind, a union of virtue". Her sentence was therefore limited to one month by king Frederick I of Sweden; he also added, that during her imprisonment, it would not be necessary to let her live merely "on water and bread."
Maria was judged to eight days imprisonment for her homosexuality. After this, they lived a quiet life on the estates of Ulrika's relatives, though often separated on different estates; letters show of Ulrika's and Maria's love for each other. Ulrika died in 1733, and Maria continued as a houskeeper until her death in 1761.
She was not the only woman in Swedish history to have lived her life as a man; in 1569, Brita Olofsdotter from Finland served in the Swedish cavalry in Livonia; in 1679, Lisbetha Olsdotter was convicted and executed for living as a soldier under the name of Mats Ersson and for having married a woman, and Carin du Rietz became a soldier at the royal guard in the 1780s. Another woman from the same war was also put on trial for having served in the army as a man; she was, in contrast to Ulrika, whipped, but continued to dress as a man throughout her life and was seen walking the streets of Stockholm dressed as a man until the 1740s, commonly called "the rider".
Plays have been performed about "The amazon of Charles XII", in 2004-2005 by Calmare Gycklare.