Q:

Why are cars allowed to unlap themselves during safety car period?

As FIA indicates in Article 39.12 (sporting regulations 2017), lapped cars may unlap themselves once cars were lined up behind the safety car.

39.12 If the clerk of the course considers it safe to do so, and the message
  "LAPPED CARS MAY NOW OVERTAKE" has been sent to all teams via the
  official messaging system, any cars that have been lapped by the
  leader will be required to pass the cars on the lead lap and the
  safety car.

What is the purpose of this rule?
I don't see any benefit to drivers' safety. It also causes unfair competition in some cases. For example, Valtteri Bottas gained a big advantage by this rule in 2017 Azerbaijan Grand Prix.

A:

There is another purpose to this rule besides lapped cars being mixed up between the leaders.
Consider this example: 
We are in the 2nd half of the race. Two drivers (let's say, Hulkenberg and Perez) fight for P10, which is effectively a fight for a Championship point. They are far from the leader (almost a lap down). Perez is currently P11 and a few seconds behind Hulkenberg, who is P10. Perez is lapping a second a lap faster than Hulkenberg and so he's almost sure to catch Hulkenberg and overtake him before the end of the race.
The race leader catches up to Perez, who gets shown blue flags and lets the leader pass him. He is now lapped. At this moment, some accident happens and the safety car is deployed. It picks up the leader and other drivers line up behind him, including Perez, who's been lapped just before the safety car was deployed and so he's right behind the race leader. But Hulkenberg hasn't yet been lapped and so he is now allowed to do an entire lap and join the end of the queue behind the safety car. Now Hulkenberg effectively has entire lap's advantage over Perez (even though it was just a few seconds before the safety car period) and Perez will not be able to close that gap. 
However, if the lapped cars are able to unlap themselves, Perez will also do an extra lap and join the queue just behind Hulkenberg, which means he will be able to fight Hulkenberg for 10th position.

A:

F1 previously had this rule but it was removed because it was difficult to manage and potientially dangerous. Now there are new safeguards involved like drivers will only be allowed to overtake once they have all passed the pit entry twice (which will allow all drivers to pit if they want), lead drivers has to stay on the racing line once the order is given to allow cars to overtake, and will be allowed to weave again, to get heat into their tyres, when informed it is safe to do so. By Charlie Whiting FIA technical delegate
View of Andrew Shovlin, Senior Race Engineer at mercedes at 2012 is that before this rule was introduced (2010 and 2011 lapped cars were not allowed to overtake prior to a safety car restart), race leader get mixed up with back markers at the end of race. Depriving the spectators of an exciting finish as it allowed some cars to build good lead while their competitors were stuck in traffic. Making it confusing for those watching as the order of cars on track was not the actual race order. 

This will have two important effects, firstly the cars directly behind the safety will line up in actual race order so when the race restarts they are in a position to overtake each other. Secondly, the majority of the field will be on the same racing lap, bunched together and with some of the cars on new tyres. Together with the DRS, this should create exciting racing in the midfield where cars on older tyres will be ahead of cars that have stopped for fresh rubber who are trying to battle their way up the order for points.”

Other source: Formula 1 reddit
This view makes it easier to understand the purpose of this rule in some way, will add more sources later.

