''So if they are legal refugees , why were they in detention in the first place''
because that is how Australia chooses to treat people whilst it processes their application for asylum
no more, no less
other countries allow asylum seekers to be in society - some allow them to work - all developed countries give them support - shelter and food etc and don't 'lock them up' -------- detain.
and as you will find when you research - there is no 'illegal asylum seekers'