An Australian politician and convicted rapist has resigned from parliament moments before he was to be kicked out, after losing a legal challenge to remain.

Gareth Ward, 44, was last month found guilty of sexually assaulting two young men, aged 18 and 24, between 2013 and 2015, and is now in custody pending sentencing.