My sister's MacBook Pro often shuts down at ~20% battery. It won't turn on again until it has been plugged in for about 10 minutes.
Your battery is in very bad condition. In those cases it's not uncommon that the remaining time is not calculated properly, and your computer will turn off (ungracefully) as soon as the battery can't provide the necessary power levels.
You should have your battery looked at and replaced. Depending on the battery age and reason of defect, this might be replaced under warranty. However, if the battery died of age, it likely isn't.
Check the battery replacement conditions here: