Those prepayment fines are basically what the bank is losing out on you. If you have a 3% mortgage and the current rate is 2%, then the bank would lose out 1% for the current mortgage period. That's the penalty.
That is not how the penalties are worded at all. Penalties are usually in the form of a percentage of a number of months' interest. I understand the fine is because the bank makes money on loans by being able to rely on them being a fixed long term, and the cancellation affects that, but it's not just the bank passing on that percentage loss directly.
Perhaps it's a country thing. Here in the Netherlands that is how the banks calculate the prepayment fine. You are allowed to pay off 10% to 20% of the original sum per year but over that amount you pay a "boeterente" (interest-fine). It's equal to the amount of money the bank loses by loaning your money to someone else over the remainder of the fixed period (10/20/30 years).
They are specified in the contract. If you sign the mortgage contract you are giving the bank steady business for the next 10 years in return of a lower rate. By breaking that 10 year agreement the bank loses money which they will charge you for.