Just get a spare key and leave it in the house or your wallet. You may have just hit the right sequence of things to make a longshot happen. You may aslo have hit the doorlock accidently and didnt realize it.
Think it through now: the KEY to your (probably locked) HOUSE is most likely going to also be LOCKED inside that running car! I've seen it happen, many years ago, when my neighbor woke up at 4 a.m. to go skiing, and she had backed into the street, then she thought about something she forgot inside her house, and when she ultimately got up the courage to knock on my door, she had tears running down her face, and said, "My car is locked and it's running with the keys in it and I also cannot get into my house!"
I don't have a total solution in place YET for my new Subaru, but on some older cars I owned, I would hide either a house key or a car key somewhere on the car. Even if you have to BREAK something to get it, it can save the day. On my Toyota, I needed to use this emergency key ONCE in 14 years, but if you live alone or live in a very dangerous climate (check both boxes for me) this can literally be a lifesaver!
The MINIMUM thing to do is at least make a COPY of your car key and leave it at a nearby relative's house. I know we all get three years of Subaru Roadside Assistance, but seriously, in some climates, having an extra key could save you from freezing to death. Even while getting gas or something simple like that, it's very easy to DROP a key and lose it into a drain or something. I like the idea of having an extra remote, which is my current solution. I carry my "spare" remote starter in my coat pocket, and it's ALWAYS with me. Note I said EXTRA key. Because worse than locking the key in the car, you may LOSE it, and it wouldn't be impossible to freeze to death where I live while waiting for Roadside Assistance.
Freak accidents happen all the time. Just this week, a tree fell onto a car not far from here and killed a teenage girl. That is just awful and maybe there's no way to prevent some accidents, but having a SPARE key is something we CAN control!