Sophie met students and teachers during her visit to a girls' school in Baghdad
Buckingham Palace said Sophie had spent two days in Iraq's capital to learn of the challenges women and girls face.
She visited a girls' school to hear from pupils about their education.
After meeting Iraq's women young and old, Sophie visited President Abdul Latif Rashid and prime minister Mohammad Shia Al Sudani.
She was praised for being the first member of the Royal Family to visit Baghdad by the UK's ambassador to the country, Mark Bryson-Richardson, whom she spent most of the trip with.
Sophie, who gained the title of Duchess of Edinburgh when her husband Prince Edward took on a new role in March after the death of Queen Elizabeth II, has said in the past that she is passionate about supporting women and gender equality around the world.
She champions a global initiative to prevent sexual violence in conflict and on International Women's Day in 2019, she announced her mission to support the UN's women, peace and security agenda, which encourage women's participation in peace processes and conflict resolution.
During her visit to Baghdad this week, Sophie gave a speech at the annual Women's Voices First Conference to promote that agenda - which says women must play a part in all efforts to achieve international peace and security.
Earlier in the day, Sophie posed for photos with students and teachers at Hitten Girl's School as she discussed education for young women in the country, and what their hopes were for the future,
She also visited a family planning centre to hear about work being carried out to support the reproductive health and wellbeing of Iraqi women, and met female business leaders to discuss the importance of women's economic empowerment.
The duchess then paid a visit to the two men running the country - prime minister Mohammad Shia Al Sudani and president Abdul Latif Rashid.
First lady Shanaz Ibrahim Ahmed was also present as Sophie was formally received by the president.
Mr Bryson-Richardson said Sophie and the two political leaders spoke about the UK and Iraq's "strong ties", as well as discussing the country's plans to give support and justice to survivors of sexual violence in armed conflict.
Sophie also had a formal task to complete, passing on a message of greeting from King Charles to the two leaders.
Mr Bryson-Richardson further praised Sophie for helping to raise awareness on the importance of Iraq to the UK as he said it was a "sign of the strength of our bilateral relationship".
The unannounced trip had been kept secret for security reasons and was made at the request of the ForeignOffice, Buckingham Palace said.
The Foreign Office currently advises British nationals against all travel to the majority of provinces in Iraq due to the threat of violent protests in and around the International Zone in Baghdad.
The UK and Iraq have been rebuilding their relationship in recent years due after British troops fought there two decades ago in the second Iraq war, which broke out in 2003.
Other royals have visited different parts of the country over the years.
When he was Prince of Wales, Charles travelled to Basra in 2004 to visit British troops fighting in the war.
A couple of years later in 2006, Prince Philip, the late Duke of Edinburgh, also made a surprise trip to see the troops in Basra.
Sophie has had a high profile this month as she helped mark the King's Coronation over the four-day celebrations.
She also delighted royal fans as she was spotted dancing to Lionel Richie as he sang his classic song All Night Long during the Coronation Concert.
Then on the Sunday, the Duke and Duchess of Edinburgh visited a street lunch in Surrey as more than 1,000 street parties and lunches were held across Kent, Sussex and Surrey.