The issue has long strained ties, with South Korea demanding stronger apologies and compensation for victims.
The agreement represents the first deal on the issue since 1965 and comes after both sides agreed to speed up talks.
The announcement comes after Japan`s foreign minister Fumio Kishida arrived in Seoul for discussions with his counterpart Yun Byung-se.
After the meeting Mr Kishida told reporters that Prime Minister Shinzo Abe offered a heartfelt apology.
"Abe, as the prime minister of Japan, offers from his heart an apology and reflection for everyone who suffered lots of pain and received scars that are difficult to heal physically and mentally," he said.
Up to 200,000 women were estimated to have been forced to be sex slaves for Japanese soldiers during WW2, many of them Korean. Only 46 of them are still alive in South Korea.
Other women came from China, the Philippines, Indonesia and Taiwan.
Japan had previously acknowledged its responsibility for sex slaves in a 1993 statement by the then-chief cabinet secretary Yohei Kono.
Japan had also resisted giving greater compensation, arguing that the dispute was settled in 1965 when diplomatic ties were normalised between the two countries and more than $800m in economic aid and loans was given to South Korea.
A private fund was also set up in 1995 for the victims and lasted for a decade, but money came from donations and not from the Japanese government.
More about: