Have you seen the birth entry for Mary to see whether parents show as married (I think you said you thought her birth was illegitimate). Apart from Mary's birth, there is another son showing to this couple born in Sorn in 1857 (James on 23 AUG 1857 Sorn). There is this entry in 1871:
Margaret Bain 53 b. Ireland
Margaret Bain 26 daughter, mill worker, b. Dalry, Ayrshire
Robert Bain 19, son coal miner b. Dalry
John Bain 15 son b. Sorn
James Bain 13 son, b. Sorn
Mary Bain daugh 9 b. Sorn
Address Sorn Village
The answer is there somewhere! I think this is this family in 1861 with the surname mis transcribed (Scotlands People will hopefully be clearer):
Joseph Barie 47, iron miner b. Ireland
Margaret Barie 45 b. ireland
George Barie 22 b. Ireland
Joseph Barie 20 b. ireland
Margaret Barie 14 b. Dalry
Helen Barie 11 b. Dalry
Robert Barie 8 b. Dalry
John Barie 6 b. Sorn
James Barie 3 b. Sorn
Address: Grocers Shop, Sorn
David, looking at these census entries, I'm not sure that Mary's birth was illegitimate
She does look to be the daughter of Margaret Snr (Ambrose). This would then fit well with John MacDonald showing as living with his Aunt Margaret Bain in that 1891 entry from earlier. These entries are all post 1855 so hopefully you should find Joseph Bain Snr's and Margaret Ambrose/Bain's death certs to help clarify and confirm.
Monica