Their release came after the International Red Cross and the Swiss government brokered a deal with the group, and negotiations would continue to bring home the rest of the girls, a statement from the Nigerian presidency said.
Around 270 girls were taken from their school in Chibok in the remote northeastern Borno state, where Boko Haram has waged a seven-year insurgency to set up an Islamic state, killing thousands and displacing more than 2 million people.
Dozens escaped in the initial melee, but more than 200 girls are still missing. The kidnapping prompted outrage worldwide and the girls' plight was publicized using a Twitter hashtag, #bringbackourgirls.
"It is the first step in what we believe will be the release of all the girls," Information Minister Lai Mohammed told reporters.
He denied reports that the government had swapped Boko Haram fighters for their release and said he was not aware if any ransom had been paid. He said a Nigerian army operation against Boko Haram would continue.
The girls were released at 5.30 a.m. and will be taken to the capital Abuja during the afternoon to meet doctors and psychologists, Mohammed said.
The Swiss government "facilitated contacts between representatives of the Nigerian government and intermediaries of Boko Haram" after a request from Abuja, a spokeswoman said in a statement.
"We have nothing to add," she said, when asked if it had been a prisoner swap.
CNN published a picture on its website it said showed several of the freed girls, wearing veils and being escorted by soldiers in Maiduguri, capital of Borno state.
Authorities said in May one of the missing girls had been found and President Muhammadu Buhari promised to rescue the others.
In the past days, the Nigerian military has been carrying out a large-scale offensive in the Sambisa forest, a stronghold of Boko Haram, which last year pledged loyalty to the Islamic State militant group.
Boko Haram controlled a swathe of land around the size of Belgium at the start of 2015, but Nigeria's army, aided by troops from neighboring countries, has recaptured most of the territory. The group still stages suicide bombings in the northeast, as well as in neighboring Niger and Cameroon.
Boko Haram published a video in August apparently showing recent footage of dozens of the kidnapped girls and said some had been killed in air strikes.
The militant group has kidnapped hundreds of men, women and children but the kidnapping of the Chibok girls brought it worldwide attention.
In the last few months Buhari has said his government was prepared to negotiate with Boko Haram over the release of the girls.