Hi,
if you complete a task and set the variables, he tries to set each variable on the task, but if the variable is not already existing, it will be created in the most outer scope. This means the process instance in case the task is related to an execution.
Currently it is not possible to set local variables on a task via the REST API. At the moment you can use therefore the following Java API methods "TaskService#setVariableLocal()" or "TaskService#setVariablesLocal()". Then it will be only available for this task and will be destroyed, when the task is completed.
I hope that helps you?
Cheers,
Roman