Answering your own question when you've found the answer is fine, and even encouraged: there's even a badge for it! The Ask Question page has an “Answer your own question” checkbox available for a reason. :)
Generally it's received very well in the following situations:
You have asked a question about an actual problem you're dealing with. Soon after asking it, or perhaps even a long time later (hours, weeks, months), you've conducted more research and discovered the answer—perhaps an existing (but unsatisfactory) answer pointed you in the right direction. You come back to share the solution and your findings.
A great example of this happening is here: What's the best magic item for punching monsters in their stupid faces?
You ran into an actual problem, and before even answering it here, you figured out the answer. The problem is nontrivial (it's not e.g. a straightforward book lookup), and you think other people will reasonably run into this same problem in practice and would benefit from what you learned. You come here to write a question about your problem and you also write an answer with the solution.
A great example of this case is here: Does the tower shield's table entry trump the text?
Whenever you write your self-answer, make sure you're still providing a complete, thorough answer, appropriately cited. Pretend the person that asked the question is someone completely different to yourself, for example, and you're writing it for them. In reality you're writing it for the numerous internet denizens who need your expert guidance. (Never ever write “btw everyone I figured it out, it's this” with no citation; that will not go well.)
When you ask a question to self-aswer (per that second bullet point), write the question how you would if you didn't already know the answer. Explain your confusion and difficulty. Channel what you were thinking a few minutes or a few days ago when this problem was initially coming up, before you solved it for yourself.
Accept your own answer whenever you're ready. (With the caveat that the system won't let you accept your own answer within 24 hours of a question.) Be open to the idea someone will in fact have a better, more correct answer than your own—strongly consider accepting that one if it arrives and answers your question to your satisfaction. Be open to the idea you might have made mistakes! Be warned: people downvote incorrect answers, and incorrect accepted answers more severely, and incorrect accepted self-answers yet more severely still.
Still avoid creating a self-Q&A trivial issues you've never really needed an answer to. “How much does a longsword cost?” is a trivially resolvable question in D&D and would get downvoted no matter who asked it, doubly so if you posted it as a self-Q&A. “How can I safely pick up Baal's Longsword of the Everflame without combusting?” might be a really good question if anyone at all asked it if the solution isn't obvious, and it will still be a good question if you ask and self-answer it, and it may be very interesting to read how that works.
Further reading on this: