OP I've been where you are 6 months ago. Exactly the same situation - I was encouraged to apply for the promotion role, but didn't get it at interview and they gave it to someone external.
I moped about it for a bit, got feedback from hiring managers - namely, the external candidate had different and more varied experiences. I realised I wouldn't get that variety if I stayed in my current role, so I applied for other roles, got one and took it.
Don't write yourself off. But you do need to look elsewhere (whether that's another role internally if you work for a big organisation, or in a different organisation).
And the irony is...the person they hired externally did the role for 5 months before moving on internally.
Another story. My OH went for an internal role working on a project that he knew inside out and worked regularly and well with the hiring manager and rest of the team. They decided to hire another internal person who had no experience of this project, had never worked with the hiring manager or team before, but came with glowing references from higher up types. OH scored 1 less point in the interview. Hiring manager decided to go for the other person based on their reputation, saying that swung the decision for them in the end, and when giving feedback to my OH said he'd ummed and ahhhed about it because of the 1 point difference. Anyway you can guess what happened - the other person did not gel with the people on the project, did not get to grips quickly enough with the project, and went off on long term sick leave with stress.