The 21 were freed before dawn in the northeastern Nigerian town of Banki, near the border with Cameroon, a source close to the negotiations said on condition of anonymity.
They are said to have been among the 276 girls and women, ages 16 to 18, that Boko Haram militants herded from bed in the middle of the night at a boarding school in Chibok in April 2014 -- a kidnapping that spurred global outrage.
A child born to one of the girls and believed by medical personnel to be about 20 months old also was released, according to the Nigerian president's office.
The International Committee of the Red Cross and the Swiss government brokered the deal between the Islamist militant group and the Nigerian government, said Mallam Garba Shehu, spokesman for Nigerian President Muhammadu Buhari.
The release is "a result of the round-the-clock efforts by the administration to put a closure to the sad issue of the (kidnappings)," Nigerian information minister Alhaji Lai Mohammed told reporters in Abuja.
"We see this as a credible first step in the eventual release of all the Chibok girls in captivity," Mohammed said.
Terms of the deal were not immediately announced, but no captive Boko Haram fighters were released in exchange for the girls, the source with knowledge of the negotiations said.
After their release, the former captives went to meet with the governor of northeastern Nigeria's Borno state, and later taken to the central Nigerian city of Abuja, Borno officials said.
"We welcome reports ... of the negotiated release of 21 of our abducted #ChibokGirls today," the Nigeria-based #Chibokgirls campaign said in a statement. "Following this development, we trust that our government will continue to work to keep the safety, security, and well-being of the other girls a high priority.
"We further urge the international community to continue to support our government's effort to rescue all other abducted Nigerians, so that parents, the Chibok community, the nation, and the world can finally put an end to this nightmare once and for all."
Nigerian authorities identified the girls as Mary Usman Bulama, Jummai John, Blessing Abana, Lugwa Sanda, Comfort Habila, Maryam Basheer, Comfort Amos,Glory Mainta, Saratu Emannuel, Deborah Ja'afaru, Rahab Ibrahim, Helen Musa, Maryamu Lawan, Rebecca Ibrahim, Asabe Goni, Deborah Andrawus, Agnes Gapani, Saratu Markus, Glory Dama, Pindah Nuhu and Rebecca Mallam.
In Abuja, the girls were met with hugs from Vice President Yemi Osinbajo.
On Twitter, Osinbajo wrote: "Dawn, dusk, almost a 1000 days. Twenty-one of our girls are back. It is my joy to welcome you home. The nation has been waiting for you."
Chibok is in Borno, where Boko Haram is strongest. The militants were believed to have taken their captives to the dense Sambisa forest, a reputed Boko Haram stronghold.
The terror group, which pledged allegiance to ISIS in March 2015, aims to impose a stricter enforcement of Sharia law across Africa's most populous nation, which has a Christian-majority south and a Muslim-majority north.
The 2014 kidnapping prompted global figures such as Pakistani education activist Malala Yousafzai
and US first lady Michelle Obama to support a #BringBackOurGirls campaign on social media.
The release comes amid an apparent split in the terror group, with ISIS introducing an alleged Boko Haram leader in the summer
even as Boko Haram's longtime chief maintained that he still was in charge.
Series of videos showed captives this year
The students' release also comes amid pressure from the girls' families on the Nigerian government to secure their freedom from whomever still held them.
The pressure increased as Boko Haram, or groups claiming to represent it, issued videos over a period of months showing some of the kidnapped students, apparently to prove to the government that some were alive and to make demands, such as the release of captured Boko Haram fighters.
This includes a recording, believed to have been recorded in December, that CNN exclusively obtained in April
showing 15 of the girls.
Another video, addressed to the girls' families in August
, shows 50 girls wearing headscarves. A father of one of the captives told CNN that his daughter and other Chibok students were in the video.
In that recording, a militant says that 40 girls were married off. The militant claims some girls were killed in a Nigerian airstrike.
A Boko Haram insider told CNN in August that despite the leadership split, longtime leader Abubakar Shekau still controlled the Chibok girls
and was thought to be hiding in the forest.
Girl found in May: I miss my Boko Haram husband
Beyond the 57 who escaped soon after their capture, the first Chibok student to be recovered was Amina Ali Nkeki, who was found on the outskirts of Sambisa forest in May.
She was with a man she had married in captivity -- allegedly a Boko Haram fighter -- and their baby daughter. The man, who told a witness that he, too, had been kidnapped by Boko Haram, was detained by Nigeria's military.
Amina Ali told CNN in August that she was held hostage by Boko Haram for more than two years, and was married off a year into her ordeal.
Ali said that she didn't know where her husband was taken after his detention, but that she wanted to be reunited with him. "I'm not comfortable with the way I'm being kept from him," the 21-year-old said at an undisclosed location in Abuja
Ali said that a dozen captives died in a bombing more than a year ago.
For a year after they were taken, the abducted girls were kept together, she said. Then some of the teenagers -- including her -- were "given" to the terrorists as wives.
She said her desire to see her mother again gave her courage to flee camp. Asked how she felt about becoming a mother herself while in captivity, her face clouded over and, speaking through an interpreter, she said: "I don't want to answer."
'Not a swap'
Mohammed, the Nigerian information minister, said that Thursday's release was "not a swap."
"It is a release, the product of painstaking negotiations and trust on both sides," he said.
According to the Global Terrorism Index released last year, Boko Haram was the world's deadliest terrorist group in 2014, before it pledged allegiance to ISIS, the terror group that controls parts of Syria and Iraq.
That year, the GTI said, Boko Haram was responsible for 6,644 deaths, compared with ISIS, which was responsible for 6,073.