I submitted a manuscript into a (maths) journal a while ago. Today I received a decision e-mail saying that they have to reject my paper because it is too specialised for their journal. I belive this is fine after 1-2 weeks of receiving my submission, but I got this answer 3 months + 1 week after I submitted my paper. Is there anything I can do in such a situation or I just have to accept that perhaps someone forgot about my manuscript for a while?
You can send it elsewhere. What you can't do is force a journal to publish your paper. The time isn't especially significant. A year or so would be a problem.
But reviewers have other tasks in their workflow. I doubt that it was forgotten generally, but some reviewer might have been busy/sick/whatever.
There may even have been some discussion about the paper, not making the decision an easy one.