Repaying debt vs Investing - Am I calculating something wrong?

I always had in my mind that if the interest in your debt is higher than the profit you can obtain in the stock market, it’s better to repay your debt. But I just run the numbers and they don’t add up, I believe I’m doing something wrong so maybe you guys can help me.

P.S. I’m not from the US, in my country we use the “French system” of repaying debt, so maybe the debt repayment in the US is different?

Imagine you have a $10,000 debt with a fixed rate of 4% for 3 years. You also have a portfolio of $10.000 in stocks (let’s ignore capital gain taxes for this exercise) that yield 4% per year. What is better, sell everything to repay the debt or continue with the monthly payments and don’t touch your stocks? In theory it should be the same, but the calculations say otherwise.
The monthly payment for a 10k debt at 4% is $295.24, for a total of $3,542.88 per year.

Scenario 1:

Years 1 to 3 you have 10k in stocks growing at 4%.
Years 4 to 10 you add $3,542.88 per year to your investments since you no longer have debt payments.
Total capital after 10 years = $43,109.44

Scenario 2:

Year 0 you sell all your stocks to repay the debt.

Years 1 to 10 you invest $3,542.88 per year since you don’t have debt payments and it grows at 4% per year.

Total capital after 10 years = $42,756.93

Even if the difference is just $350, there’s still a difference and I don’t understand why. On top of that it’s not linear: If you increase the duration of the debt or the interest rates, the difference grows.

Can anyone help me understand what is happening? What am I doing wrong? Thanks