From what you describe I think you are looking for build modes.
In Lazarus go to Project Options and select Compiler Options.
On the top you will find a label "Build Modes" with a drop down next to it and a button "...". Click that button.
In the newly opened dialog now click "Create Debug and Release modes" in the upper left corner, this will automatically create two new modes, Debug and Release, with sensible configurations for each. You can now close the dialog by clicking "Ok".
Next go to "Custom Options" in the "Project Settings". Make sure "Debug" is selected above in the Build Mode dropdown (it is visible on every entry under "Compiler Options" and press the "Defines" button on the left.
In the newly opend dialog enter "DEBUG" into the Edit at the top and click add. Make sure the checkbox in the List that now was added is checked. Click Ok and in the "Custom Options" Memo "-dDEBUG" should have appeared. If that is the case you can close the Project Options by clicking Ok.
Now you have two new Build modes, Debug and Release, and during Debug, the Definition DEBUG will be set, meaning if you compile your application in this mode, the following Directive will evaluate to true:
{$IFDEF DEBUG}
WriteLn('Debug only code');
{$ENDIF}
To change the build mode, left of the run icon (the little green arrow) you find a button with a gear on it, the tooltip reads "Change Build mode" This icon consits of the main button and a little down arrow to the right of it (directly left of the Run without Debugger symbol). If you click this (the arrow down button), a drop down will appear where you can choose your build mode.
When you run your application from Lazarus, the selected mode there will be used to build. So you want to use Debug, because you want to debug it while in Lazarus.
To now publish your application go to Menu -> Run -> Compile Many Modes. Select "Release" and click Ok. This way the Release build is done without changing the current build mode, so whenever you use the Lazarus Run feature (e.g. the green arrow) still Debug will be executed.
You can test this with this little Application:
program Project1;
{$mode objfpc}{$H+}
begin
{$IFDEF DEBUG}
WriteLn('Debug Mode');
{$ELSE}
WriteLn('Release Mode');
{$ENDIF}
end.
As Debug is selected as current build mode, running the Application from Lazarus will print out Debug Mode. If you go to Run -> Build Many Modes -> Release and execute the produced executable (by navigating to the folder it is compiled to and execute it, e.g. by double click) it will print Release Mode.
Note: If you want to have a clear distinction which build is the Debug and which is the Release build, simply go to Project Options -> Paths. Now select Release in the dropdown above and under "Target file name (-o)" select something like "bin/$(TargetCPU)-$(TargetOS)/Release/projectName", then select Debug in the drop down and add "bin/$(TargetCPU)-$(TargetOS)/Debug/projectName" to the "Target file name" field. This way whenever you are building your app, the executable for Debug will go into PROJECTDIR/bin/arch-OS/Debug and when you build release with Build many modes it will go into "PROJECTDIR/bin/arch-OS/Release" (where arch-OS could be for example X86_64-win64 on windows 64 bit). This way you don't accedentally override your release build with a debug build
Note 2: Changing the DropDown in Build Modes, changes the currently selected build mode. So when doing changes to Release in the Project Options, you want to change it back to Debug afterwards
Note 3: While you are editing, Lazarus will automatically deduce which ifdef will be compiled and what not, meaning it won't be able to give you help (sensible auto completion and stuff) while you are working in the else branch of the DEBUG ifdefs. So if you are writing code for this, you probably want to temporarily switch build modes while editing, so you can use the full features of the IDE