It's just that the 5.8% comes as a surprise. Why is this??
Well it comes from a much smaller change in the interest interest rate on a much larger total balance.
Now what's interesting is what happens if you run the scenario at to the end of 10 years (so from the start of year 8, you put 1130 into the market in the 7 year mortgage scenario and continue to put in 343 in the other scenario).
At the end of 10 years, both houses are paid off, but (if we assume a 5.5% nominal annual return*), you end up with $54,318 in stock market investments in the 10 year mortgage scenario, but only 44,031 in the 7 year mortgage scenario.
In other words you come out $10,287 ahead with the 10 year mortgage after a decade, even though you're paying a higher interest rate than on the 7 year.
*Which would probably be something like 2-3% real returns given current inflation rates. This would mean it had been an extremely crummy decade for the stock market, but not great depression or 1970s bad.