Hello Nika,
"What a pity" is usually a verbal reaction on a sad, disappointing story told. Saying it indicates that someone understands the suffering of others and shares it. It reflects a feeling of sorrow and sympathy for the misfortunes of others.
It expresses consolation and is equivalent to saying "That's too bad".
A - Both his parents died when he was 3 years old.
B- What a pity!
It could be also used in a sarcastic manner.