Great question, and one in which you will find much dissent. There are several major positions. Two illuminating articles to give you background are:
The only book which I own which is a primary source is Kripke's Naming and Necessity and is considered an essential read if you defend a posteriori necessity, which is a form of metaphysical necessity.
Brief Background on Necessary and Certain Knowledge
Historically, the view was that necessary truths were primarily in the domain of a priori knowledge; philosophers in the olden days would say things like "2+2=4" must always be true! This appeal to a prioriticity is largely conducted by adducing logical and mathematical propositions as being true irregardless of the individual. So, this reconciles very strongly with the appeal of rationalism in the original sense that conceivability and introspection were certain forms of knowledge, and the products of those knowledge were irrefutable because the truths were objective. This complemented a very long standing school of thought that says there are certain things that are thought that are actually real and exist albeit in some removed plane not in contact with our physical plane but that our minds nonetheless have access to. Remember, in the time of Descartes, neurology hadn't even been invented, and it was quite a mystery what thought was. These sorts of positions are considered realist positions and the most famous of is Platonism (SEP).
In response to the notion that there is certainty in rational thoughts came the likes of Hume and others who formed the backbone of an empirical movement. These philosophers emphasized the fallibility of reason and thought in general. One of the oldest and most fundamental questions in philosophy, according to professional epistemologists like Robert Audi, is that of skepticism. How can we know anything for sure? Descartes's famous "cogito ergo sum" was a declaration that introspection was the source of certainty and thus necessity. The empiricists pushed back on that and argued that the senses and the mind could be deceived. Thus, to this day, there's a certain tension between belief that reason is certain, and that it is uncertain with rationalism on one side and radical skepticism arguing there's no knowledge at all. According to the IEP article above, most professional philosophers today accept a fallibilist notion of knowledge particularly since Gettier threw epistemologists into the lurch with his elegant problems that said knowledge is not true, justified belief.
Saul Kripke
Kripke is a giant in contemporary logic and philosophy. He did work on formal modal and semantic theory, and is known for his attack on a type of theory related to proper names, a topic that Bertrand Russell and others worked very hard to secure certain theory in which are related to the classical question of the nature of equivalence and identity. His attacks were tremendously influential according to WP:
Such arguments seem to have convinced the majority of philosophers of language to abandon descriptive theories of proper names.
As a byproduct of those attacks, he made a famous argument involving H2O that empirically speaking, certain things are certain by virtue of them being empirical facts, and thereby challenged the orthodoxy of a prioriticity being essentially a necessary condition of necessary claims.
Modal Rationalism and Modal Empiricism
According to SEP, the modern day landscape of positions on modality and possible worlds is very complicated and many philosophers, such as the famous David Lewis, took in all sorts of directions like his modal realism which basically claims that imagining of possibility equates to establishing the concrete reality of other worlds:
Most comprehensively in On the Plurality of Worlds, Lewis defended modal realism: the view that possible worlds exist as concrete entities in logical space, and that our world is one among many equally real possible ones.
What is important to know is that today, there are those who prefer the traditional theses that a priori reasoning is a firm and largely sufficient condition for establishing necessary truths. In fact, such a position seems a reasonable extension however of traditional philosophical positions still. But since Kripke certainly, there's been a push back with some philosophers taking the notion of an empirical basis of necessity and running with it (PhilSE).
So, what 'possible worlds' and 'necessary and contingent means' is a somewhat disputatious topic, and whether you're an anti-realist or realist at heart presages your interpretation of these things. That's why I pushed back on your question on Pegasus about necessary truths. Unfortunately, there are some philosophers who are unable to separate their personal beliefs from more descriptivist beliefs about right and wrong philosophy, and reject pluralism because their gods tell them, or they're too smart to be wrong, or they simply don't accept fallibilism as a basic premise of knowledge. Some philosophers reject the synthetic-analytic divide in the absolute, and other the a priori-a posteriori distinction.
What is a Necessary Truth in a Possible World?
There's no universal consensus on this, so you'll get more than one reasonable answer. From a position of mathematical constructivism or empiricism, both truths and truth systems are products of the mind and are constructed, perhaps using non-classical logics, like intuitionistic logic even formally. When you ask:
there could be some world where mathematics was defined differently rendering it to be a possibly true proposition?
It comes down to 'possible world' and 'true proposition'. For me personally, I go the route of the representational theory of mind (SEP) and rely heavily on nominalist and constructivist themes. Language is a product of the mind, and it describes internal experiences which are products of embodied cognition. Thus, a possible world is part of a truth-conditional construct that can be represented by a model-theoretic object. I'm a logical pluralist, so one needn't even require the Law of the Excluded Middle. Truth, logic, and possibility are ideas, concepts and thus there's no one-true-position on modality. Lewis's modal realism is a defensible argument with presumptions I just happen to reject. Could someone teach or design a computational agent to use and believe 2+2=5? Sure. It's a fact it can be done. The question then is more a question of utility. Would it be of any value? Certainly not if you're trying to build an accounting system or train a child to do arithmetic and function in the real world. In that sense, the a priorticity of "2+2=4" has build in normativity. There are no gods to hand down truths on high in these matters. We prefer 2+2 is 4 to is 5 because it's more elegant, and more importantly leads to closure of operations. Hence, in my view "2+2=4" is a contingent truth, one my intuition and reason defend because I want my number systems to obey closure. I could easily define '+' piecewise anyway I want and even be consistent with physicalist models.
So, you'll find lots of people who will give you pet theories or explicate the theories from the canon on these matters, but any argument put forth is just that, an argument. Whether or not you are moved by it is what philosophy is all about.