When confronted with news that could either be good or bad, pick the response that is most common.
Pregnancies, even unplanned, are normally met with cheer in the US.
Congratulate them. That's just expected behavior. If you're wrong, they will simply tell you how they really feel.
If you ask if it was planned, it could make them feel judged or get demoralized. If it it actually was an accident, your congratulations really isn't going to do them harm, as they are probably already stressed enough.
Note: This works with all news.
For example, if someone informs you that their grandparent died, you respond with comfort for their sadness instead of asking "Oh, did you and your grandparent get along?".
When in doubt, go with what's expected in your culture.