that SUCKS bro, especially if you run out of balls or something and then have to either give it up or reset and lost progress. I did this with Latios in black 2, luckily i managed to catch it, but damn was that an ordeal
If you defeat it trying to catch it (and it may apply to losing) you don't get a second chance (although that's somewhat changing), I would assume story legendarys stay till you beet/catch them but even then it's quite a hassle keep switching pokemon (so you lose without attacking) and getting back to the legendary.