The woman, Saja al-Dulaimi, was among a group of Islamists that the Lebanese government exchanged for Lebanese soldiers held captive by al Qaeda-affiliated al Nusra Front, the security official said on condition of anonymity.
Dulaimi, who reportedly has been divorced from Baghdadi for more than six years, had been in Lebanese custody since her arrest in December 2014 as she was trying to enter the country from Syria
The prisoner swap, following lengthy negotiations, happened Tuesday in the Lebanese town of Arsal, near the Syrian border, Lebanon's official National News Agency said.
Al Nusra Front released 16 Lebanese soldiers who were kidnapped from Arsal in August 2014, the news agency reported. Lebanon
also is receiving the body of slain soldier Mohammed Hamieh, who was among those kidnapped, the agency reported.
There was no immediate indication that Dulaimi's release was for the benefit of ISIS, an al Nusra rival.
Dulaimi has a brother who is a senior al Nusra figure, and her new husband also is suspected to be involved in the group, a separate Lebanese security source told CNN on condition of anonymity.
Qatar said that it mediated the exchange at the Lebanese government's request and that Lebanon released 25 prisoners, including 17 women and their children, according to the official Qatar News Agency.
Source: Woman's daughter also freed
Also released by Lebanon on Tuesday is Dulaimi's daughter, a girl believed to have been fathered by Baghdadi, the second Lebanese security source told CNN.
The girl will be free to travel outside the country with her mother, the source said.
During her detention in Lebanon, Dulaimi denied that Baghdadi is the girl's father. But Lebanese Interior Minister Nohad Machnouk said last year that DNA testing, using samples from Baghdadi that were sent from Iraq, established that the ISIS chief is the father. While the mother was detained, the girl was being held in a child care center, Machnouk said.
After her release Tuesday, Dulaimi stressed to Al Jazeera news network that she is Baghdadi's ex-wife
and that she planned to head to Beirut before departing to Turkey.