Belinda Bencic needed six match points to upset Jelena Ostapenko and seal the first Grand Slam victory of her return from maternity leave.