Why is the variability in faculty salaries between STEM fields so high? Checking the data from some public universities, I noticed that the variability in faculty salaries between STEM fields can be extremely large, even within the same university and for the same position. 

For example, at the University of Michigan assistant professors in mechanical engineering generally make around $95k a year, while for Math the same position pays around $55k.

Why is this range so large?
Every job position is it's own little economy, and to understand it fully we basically need a wide-ranging understanding of economics. But we can at least hit the most obvious and easily understandable points, so long as we are clear that each position is distinct and unique in it's own right.

Let's say you need to hire a Professor of Basket-Weave Engineering. How much do you pay them? Well, I say we offer to pay them a $1. Ah, but that would be below minimum wage requirements generally, so that'd be illegal...ok, let's pay them $20,000 a year then.

Why pay them any more? Well, in short, we have a number of problems:

1) Quality of candidates - the best in the field just aren't willing to work for so little, they likely won't even apply even if they were rich because such a low salary indicates a lack of respect and seriousness of the position. So you likely won't get your desired quality of candidate.

2) Quantity of candidates - with such little pay you probably just won't get enough qualified candidates to seriously interview.

3) Actual acceptance - even if you get enough good candidates and make an offer, there is a good chance they won't accept at all because they have better offers.

4) Perception - national rankings and general public perception can be involved. Do you want to become known as the institution with McProfessors who are paid in beans? 

5) Turn over - even if you get good people to join, they will likely leave when they get a drastically better offer. Even the most die-hard of loyalists will have trouble turning down an offer that's 4-10 times the salary they get now.

But why are similar positions paid so differently?

Each of the simple 5 conditions I outlined above vary by individual position. A Computer Engineering professor is just not the same as a Chemical Engineer - they are both engineers, but they have very different market factors involved.

Let's say they start off the same though, and you are able to hire and retain both at the same rate of $50,000 a year.

Now let's say IBM wants to put someone in charge of a next-generation computing research lab - will they want a Computer Engineer or a Chemical Engineer? I think it's pretty clear that they may very well have a preference for one particular type of engineer. The research produced could earn them billions, so they'd be happy to pay easily $500,000 a year for the right candidate. Not to be outdone, Intel, Microsoft, Google, Oracle, and a dozen other big-name computer companies also want the same kind of candidate, and all are willing to pay the same.

Now you have a problem. If you want to hire a Chemical Engineer, nothing has changed - $50k seems to get people just fine. But if you want to hire a Computer Engineer, you are now effectively competing with dozens of companies willing to pay 10 times what you pay.

One method of dealing with this is to ignore it. Sure, you'll lose the most money-motivated or even the best candidates to companies, but you'll still be able to get what you want. But what if more and more companies want computer engineers? What if it gets to be that there's a shortage of above-average candidates, and so industry will throw stupid amounts of money at any computer engineer who seems even vaguely above-average? You'll be left with only below-average engineers or ones who are outright hostile towards industry. If you expect to get grant money and support from industry partners, you are going to be in trouble either way.

So it might turn out that it easily makes sense to pay $200,000 a year to get a Computer Engineer (because, frankly, you have no real other choice anymore), but why would you pay any more for a Chemical Engineer? Their market hasn't changed any, so why waste money?

Complements and Substitutes

But now we also realize something interesting. While the tech companies prefer computer engineers, they realize that IT engineers have similar knowledge and work almost just as well. They might still prefer computer engineers, but with less demand or less pay they will make use of them too (even if they were still working slightly outside their field!).

Now you will find, thanks to the concept of substitutes, that other fields are being effected by the tech bidding wars. Pretty soon you'll be paying $150,000 for IT engineers. But what if tech companies realize that, sure they prefer the specialized knowledge of computing and IT people, but research is research - why not identity suitably brilliant Chemical Engineering PhDs and hire them? Sure it might take them a little longer to get up to speed in the field, but whatever - they may still prefer other fields, but they'll take what they can get. 

Next thing you know within a few years you went from paying everyone $50k to having to pay no less than $200k for a Computer Engineer, $150k for an IT Engineer, and $100k for a Chemical Engineer - minimum! Any less and all the really desirable candidates pass your institution over with ease as the other offers they get are just too high. Meanwhile professors whose primary role is teaching CS101 still command little more than that $50k, because industry just isn't super excited about people who can teach entry-level courses.

And what if other institutions start competing for top candidates, just like the industry has been doing? As Artelius points out, Baumol's Cost Disease is a hypothesis that seeks to explain increases in salaries when productivity of the job itself hasn't changed, if you are interested in further reading.

This little fictional story isn't far off exactly what has happened not only in Engineering fields, but all fields. My own adviser once pointed out to me that even people who do the same job, in the same field, get paid differently based upon what program they earned their final degree in - Psychology faculty just get paid less than Business or Engineering folks, even if they both work in the same exact applied research field.

People Are Different, Want Different Things, and Value Things Differently

Ultimately this is the bottom-line - people with similar titles are not paid the same because our system is based upon relative values of individuals, groups, and the economy as a whole. People are different, want different things, and don't assign the same universal value to all things. One agent might not be willing to buy something for more than $10, while another agent considers it a steal to get that thing for less than ten times the price!

Even more importantly, no one really wants to pay more than they have to to get something they want. It is almost universally true that if someone pays X for something, it's because they didn't think they could get an equivalent thing for much less.

And we got to this wide variance in salaries even while ignoring concepts like price-based value perceptions (national rankings that weight faculty pay as part of their rankings, for instance), vanity/political capital (deans or department heads who believe larger pay and budgets means more esteem and rank), paying a premium up-front to reduce turn over due to cost considerations (don't want someone in charge of a big funded research lab to leave every few years, so you make sure they are considered extra-well-paid so they don't jump ship), or even differential income value of a position! Not all positions bring in the same amount of community support or grant money, nor do all departments get the same support from higher-level administration (MIT just might not care that much about their history department but considers their robotics department mission-critical, for instance).

But even if we ignore all these factors, the simple 5 factors at the beginning of this answer are more than sufficient to explain a difference in faculty pay, to say nothing of differential industry valuations/competition. The true reason faculty salaries differ so much is due to a combination of all these factors and more.