TORONTO—Major oil companies, under pressure from investors and environmentalists, are fleeing Canada’s oil sands, the fourth-largest oil reserve in the world and by some measures one of the most environmentally unfriendly. Investment in existing projects has stalled, and banks are refusing to fund new ones. Nevertheless, oil production there is expected to continue for at least two more decades. Local companies have stepped in to keep working the existing mines and wells. Last year, the oil sands were on track to deliver more oil than ever.